Good practice for unit test properties?

I still understand the whole concept of TDD. Should I write property tests? Should I write tests only for properties that contain sufficient logic? Any thoughts or examples on this would be wonderful.

+4
source share
6 answers

Writing a test for the public getter of a private field will not give you much if this getter does nothing but return your personal field. But if it contains some logic (or just something that might fail, such as converting your private Int32 field to Byte), testing this property starts to make sense.

+4
source

Check out those things that have a reasonable chance of failing - you don't get extra confidence by checking properties without logic outside of get / set.

+4
source

A simple rule that I use when doing TDD is to always write down tests that don't run.

If the test fails first, this is a good test for TDD. This means that something has not yet been implemented or not implemented as it should be. Then you can change the code to make it pass. A test that passes successfully first is a bad test. You don’t even know if it succeeded because you made a mistake by writing a test, or because what you are testing already works.

If you can fail a test using properties, then write tests for these properties. Usually, you should start by testing the test setter or getter before implementing it. A not implemented setter or getter may look trivial, but makes the test fail. And why would you write a line of code even by the installer or the receiver, if it is not controlled by a test?

Other types of tests, such as tests, such as documentation showing how to use the API, are also very useful, and good flexible practice, but this is not TDD. TDD is trying to actively try to break the code until you can no longer use it. Then you run the functional tests, and if you push the unit tests quite hard, and if there isn’t any kind of integration or system problem along the way, everything should be fine.

+4
source

Usually I write unit tests for accessories. This does not add much when they are written, except to maintain coverage statistics fairly. But if someone adds “enough logic” later to the production code, the tests will already be in place to catch any errors.

In addition, it is the work of moments to write a test to check the value returned by the recipient.

+2
source

Instead of thinking of them as tests, think of each test as an example of how someone can use your code.

Instead of just testing properties, think about behavior that changes when properties have different values, and give an example of class behavior in each significant context.

If this is really just a data property that you can verify by checking either with automatic acceptance tests or manually, perhaps with a tester. Otherwise, do not worry about testing each method or each property - just show how you can use the code and how you expect its behavior.

+1
source

Think of the TDD and Unit tests as a way to “see ahead,” you write the test in a way that, in your opinion, would be best for your public signature, your classes.

0
source

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


All Articles