4

I am of the Idea that a test should only have one assert.However at times is difficult to decide if i need to be strict about it or depends.

Lets take an example I have a customer with address. The Address Class has City-Street-PostCode-Country etc... properties

I would like to test if when Creating a customer the address gets populated.

Shall I create one test per property or many asserts

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 normally do when testing a method and is important that you know that properties have been filled as they are going to a third party ?

thanks for any suggestions

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
user9969
  • 15,632
  • 39
  • 107
  • 175
  • I believe this has been asked before on SO... – Mitch Wheat Jul 20 '11 at 05:11
  • That syntax is a bit wonky. Is it a typo? Did you mean Assert.That(whatever, Is.EqualTo(x)) ? – Ritch Melton Jul 20 '11 at 05:16
  • 6
    Personally, my view here is "to hell with that"; I'm interested in tests that ***get the job done***; the aim is not to have beautiful pure tests that are a shining pillar of light. Any error there is going to tell you (simply via "expected London" or whatever) which sub-component failed - and if you fix that and another pops out -so be it. If there is any uncertainty, be sure to use the `message` parameter so you know which line broke, but again - the stack trace will usually tell you exactly where. – Marc Gravell Jul 20 '11 at 05:17
  • @Ritch Melton: There are many unit testing frameworks, each with their own fluent API. – Jason Down Jul 20 '11 at 05:18
  • @Jason - Uhh ok. I'm pretty sure that PostCode is not equal to a Equal constraint. – Ritch Melton Jul 20 '11 at 05:19
  • @Ritch Melton: The code sample is the Microsoft Unit Testing Framework syntax, iirc. Ow wait.. haha, looks a bit off indeed. – Anton Jul 20 '11 at 05:21
  • @Ritch Melton: My point was not whether or not the syntax was correct (I agree it looks incorrect). I just meant that the syntax is not important in this question because he is not referring to a specific framework (and is not asking why it won't compile or anything along those lines). – Jason Down Jul 20 '11 at 05:21
  • @RichMelton.Is the Nunit Fluent Api from their example they do Assert.That("Hello!", Is.Not.EqualTo("HELLO!")); – user9969 Jul 20 '11 at 05:23
  • @JasonDown.Exactly the syntax is not the issue here just coded on the fly.My point is how many assert I should put – user9969 Jul 20 '11 at 05:24
  • Right. Assert.That() versus Assert.AreEqual(). Different things entirely. – Ritch Melton Jul 20 '11 at 05:24
  • @RichMelton,you are right.Sorry again typing on the fly – user9969 Jul 20 '11 at 05:28
  • @MarcGravell .Thank you I have the same thinking but just wanted to get some opinions. – user9969 Jul 20 '11 at 05:30
  • @MarcGravell + OP: I think the same. What is the most beautiful testing procedure if it annoys you so much that you soon skip writing _any_ test. – Sebastian Mach Jul 20 '11 at 06:31
  • The idea that each unit test should have only one assert is [impractical and short sighted](http://stackoverflow.com/a/20300843/545127). – Raedwald Apr 05 '16 at 11:56

4 Answers4

3

I would say, it depends. In your case I don't really see a problem with it as your are basically testing a single piece of functionality (creating a customer). Even NUnit has a shortcut for this type of thing via the TestCase attribute.

Jason Down
  • 21,731
  • 12
  • 83
  • 117
0

In that case you can get away with many Asserts, one big reason being if one fails in a test your test will fail but you will also know which Assert failed. It depends on the testing granularity you want to have. If you want to test the code that maybe fills in that object with info from a database you might want to have multiple asserts. If a major part of your code is getting the address through some other means then maybe you want to test the address section separately. It all really depends on the situation. Usually you want to see where the majority of your logic is and what needs to be tested.

Jesus Ramos
  • 22,940
  • 10
  • 58
  • 88
0

The most important thing with testing is that there is testing. Any test is better than no test. If some fancy rules dictate you the exact way how to do it nifty, what's its worth if it annoys you so much that you soon don't write [real] anymore?

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130
0

I'm also having idea "One test" -> "One assert".. but!

If the asserts are related to the same Test case, there are no issue to put more. Like your example above, I don't see if you just check the Full address line in one case with 3 asserts;

Alexander Beletsky
  • 19,453
  • 9
  • 63
  • 86