Unit specific value testing

Consider the following code (from a requirement that says 3 is special for some reason):

bool IsSpecial(int value)
   if (value == 3)
      return true
   else
      return false

I would use unit test for several functions: one of them was called TEST (3IsSpecial), which claims that when passing 3, the function returns true, and the other, which passes some random value other than 3, and claims that the function returns false .

When the requirement changes and say that now it becomes 3 and 20 are special, I would write another test that checks that when called from 20, this function also returns true. This test will fail, and then I will go over and update the if condition in the function.

Now, what if in my team there are people who do not believe in unit testing, and they make this change. They will directly jump over and modify the code, since my second unit test may not test 20 (this may be a random int choice or have some other int hardcoded). Now my tests are not synchronized with the code. How can I make sure that when changing the code, some unit test or others do not work?

I could be doing something grossly wrong, so any other methods to get around this are also welcome.

+3
source share
4 answers

. , Not3IsNotSpecial -3 . "".

.NET () . , . :

Contract.Ensures(value != 3 && Contract.Result<Boolean>() == false);

, -, TDD , . , , . .

, , , - . . Not3IsNotSpecial (, IsSpecial(x => x != 3) == false)), . , , . , SUT. ​​, .

+2

, - , . , IsSpecial, , , .

. , , , . , . , .

, , , , .

, . , .

+1

, , - , 3 - , - . - , .

, , . enum , . , .

, :

bool IsSpecial(int value)
   if (SpecialValues.has(value))
      return true
   else
      return false

Values ​​ :

enum SpecialValues {

(3), (20)

   public int value;
}

. , , ,

+1

, , , :

  • 20 may be some valid condition for verification based on knowledge of the business domain. Writing BDD-style tests based on knowledge of a business problem may have helped you to clearly catch it.

  • 4 may have been a good value to check because of its status as a boundary condition. Probably, this could change in the real world, therefore, most likely, it will appear in the full test case.

0
source

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


All Articles