Intent behind unit tests - what should I aim for?

Today I wrote a series of tests around a method that takes an input value and returns a different data array depending on whether this value passes (internal check), i.e.

[TestMethod] 
public void IsValidForValueFour()
{
    var result = myComponent.Validator(4);
    Assert.IsTrue(result[0], "Blah");
}

The method Validator()basically searches in a (hard-coded) table stored confidentially in myComponent.

This was wrong. I have effectively tested the values ​​in a private lookup table. Do I have to worry about the values ​​that were being transmitted and exited? Should I test the length of the output array, not its contents? Or is it right to check for specific answers, given some input cost?

In short, what should be the intent behind the unit test as follows?

+3
source share
6 answers

This is useful because now you know that the function returns what it should.

In the future, if you change the implementation, you can be sure that it still does what it should, as long as all the tests pass.


How should you check such things? It's up to you how many tests you want to reform and what value you consider the result of such tests.

+2
source

A unit test should test a small piece of code, for example, one object or even one method, and should look for ways in which the method / class can break. IMHO, A unit test that never breaks may also not exist .;)

+1
source

, "Blah" 4, , !

+1

MyComponent.Validator(4) , , true, . , . , , , , , : - , , .

Validator() , . .

0

, , , , . . , , . , ( , , , ) , .

, , , ( , ), , , , .

0

If this is important in terms of requirements, you should check this out. If it is important that the value four should return "Blah", then you have a valid test. But I will also definitely extract number 4 into the meaningful variable name and pass it to the validator. Therefore, anyone who reads your test will know why you pass the value four. If you are testing some kind of algorithm, try using a tool like Pex to create input for you. It also ensures that your logic in SUD (System Under Test) will behave as expected.

0
source

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


All Articles