How to separate unit test methods?

Imagine I have a method:

void Method(bool parameter){
    if(parameter){
        // first case
    } else {
        // second case
    }
}

What is your preferred unit test organizational method?

Option 1:

void MethodTest(){
  // test first case
  // test second case
}

or

Option 2:

void MethodTestFirstCase(){
  // test first case
}

void MethodTestSecondCase(){
  // test second case
}
+3
source share
7 answers

In this case, I would check the two separately.

Having said that, I am not dogmatic about the approach of "only one test per test." Sometimes it just makes it more pragmatic to test several things in the same test - in particular, if getting to one endpoint means going through another point, sometimes it’s good to combine the two.

In this case, you will really test two separate things, and not one on the way to the other, so I would separate them.

+6
source

Option 2

. , .

, . , .

+4

2 .

, Unit test , , , , - .

, , , .

+3

1, , . 2 ( ), , .

2 , , , , - .

+2

3:

[TestClass]
public class When_parameter_is_a {
    setup() {} // arrange
    execute() {} // act
    [TestMethod]
    this_should_happen() {} // assert
    [TestMethod]
    this_should_happen_too() {} //assert
}

[TestClass]
public class When_parameter_is_b {
    setup() {}
    execute() {}
    [TestMethod]
    this_should_happen() {}
    [TestMethod]
    this_should_happen_too() {}
}

. BDD- (Behavior Driven Design) , "" .

+1

.

, , , - , . , , , . , .

+1

() , . () . , . "" , . , "": , , - . , , , . , .

0

Source: https://habr.com/ru/post/1711697/


All Articles