Unit testing is the same method, but for different test data

This is a fairly simple question regarding unit testing.

I have a way, for example. GetOrderDetails, which calls the repository to receive order information. I have a false repository that can be configured to return responses to stocks.

To test the GetOrderDetails method, at least I will use the following cases -

Repository call error

  • with error code
  • with the exception

Successful repository call

  • returns zero result
  • returned one result
  • returns more than one result

Do I have to write one testing method for testing the above scenarios or should it be a separate testing method for each of the above scenarios?

, , , 1. 2. 3. , . , ..

?

+3
3

unit test . , if switch , , . , unit test , ( , ,...). unit test :

// arrange
// -> prepare mock objects, properties, setup expectations

// act
// -> invoke the method your are testing

// assert
// -> assert on the results returned by the method your are testing
+7

, , , , , , MethodName_StateUnderTest_ExpectedResult ( , / Roy Osherove, ). , , , . , unit test .

+2

, . , .

  • . unit test . , - . , .

  • , - . , 5 , 40 , - . , 6 .

  • . . , . , , .

  • The most complex logic I've ever put in unit test is the for loop. And this is only to initialize a simple array, if necessary for something specific to the test. I always avoid assertions in unit tests. But if you use data-driven tests, the for loop is great for feeding data to your tests if you need to. But if the for loop is small, feel free to expand the loop as well.

I hope some of these tips help.

+2
source

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


All Articles