Skip to content

Test Smell: it is a not a good practice to use the loop in the test  #1617

Closed
@TestSmell

Description

@TestSmell

Hi!

We notice that you use the loop structure in your test cases.
For example, testOracleJoin2_1() in SelectTest.java
截屏2022-08-17 下午7 13 54

However, using the loop in test cases is not a good test practice.
We analyzed the relevant Stack Overflow posts and summarized four potential negatives it brings:

  1. Loops make the test case more complex
  2. In most cases, a loop can be replaced with a data-driven test that is more readable.
  3. Loops break the assert-for-one-thing thumb rule. I don't mean a single assert statement.
  4. When a test fails, knowing the reason is more complicated.

Solution:
To avoid using the loop in the test, JUnit provides an annotation (i.e., @ParameteredTest), enabling a test case to run multiple times with different parameters.

@test
@ParameteredTest
@valuesource(Strings = {"(+)", "( +)", "(+ )", "( + )", " (+) "})
public void testOracleJoin2_1(String value) throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("SELECT * FROM tabelle1, tabelle2 WHERE tabelle1.a" + value + " = tabelle2.b", true);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions