How many statements do you put in a test?

I am an Idea that a test should have only one statement. However, it is sometimes difficult to decide whether I need to be strict on this or whether it depends.

Let's take an example, I have a client with an address. The address class has City-Street-PostCode-Country, etc. The properties

I would like to check if the address will be filled when creating the client.

Should I create one test for each property or many statements

Assert.That(customer.Address.City,Is.EqualTo("London")); Assert.That(customer.Address.Street, Is.EqualTo("StreetOne")); Assert.That(customer.Address.Postcode, Is.EqualTo("MyPostCode")); 

What do you usually do when testing a method and it is important to know that the properties have been filled in as they go to the third party?

thanks for any suggestions

+4
source share
4 answers

I would say it depends. In your case, I really do not see a problem with this, since you are basically testing a single functionality (creating a client). Even NUnit has a shortcut for this type of thing through the TestCase attribute.

+3
source

In this case, you can leave with many Asserts, one big reason is that if you fail in the test, your test will fail, but you will also find out which Assert failed. It depends on the test granularity you want to have. If you want to test code that might populate this object with information from a database, you may need a few statements. If the bulk of your code gets the address through other means, you might want to check the address section separately. It all really depends on the situation. Usually you want to see where most of your logic is and what needs to be tested.

0
source

The most important thing in testing is testing. Any test is better than no test. If some fancy rules dictate to you the exact way, how to make it elegant, that it is worth it, if it annoys you so much that you will not write [real] anymore?

0
source

I also have the idea of ​​"One test" β†’ "One argue" .. but!

If the statements are related to the same test test, there is no problem to put more. Like your example above, I don’t see if you just check the full address bar in one case with 3 statements;

0
source

All Articles