Is there any value in unit testing of automatically implemented properties

It seems exceptionally hard, but by the rule should be checked something publicly available, whether to test automatically implemented properties

Customer class

public class Customer { public string EmailAddr { get; set; } } 

Tested

 [TestClass] public class CustomerTests : TestClassBase { [TestMethod] public void CanSetCustomerEmailAddress() { //Arrange Customer customer = new Customer(); //Act customer.EmailAddr = "foo@bar.com"; //Assert Assert.AreEqual("foo@bar.com", customer.EmailAddr); } } 
+7
c # unit-testing tdd arrange-act-assert
source share
5 answers

What happens if you switch from automatically implemented properties to something completely different? The test only seems redundant because you know an implementation that you should not consider when writing tests.

Of course, this depends on the likelihood of your actual implementation change coming soon.

+7
source share

I usually consider any code that does not perform any action that is not worth testing. This usually means that I am not testing properties (automatically implemented or not), because they just set or return a value.

This will change if the getter or setter of the property does some kind of “work”, for example, possibly returning one of two (or more) values ​​based on the state of some other field / property / independently.

If you haven’t seen this, I highly recommend the Roy Osher book on unit testing . It addresses all sorts of questions "what to test and when," including this one.

+10
source share

It depends on whether you verify that the API matches the expected set of properties, or if, as you demonstrate, you simply check the access and setting of the properties.

In the example you give, I would say no.

Update

Conforms to the expected API; if you access properties indirectly, say in a mirrored / dynamic environment and you use access calls to properties and methods to verify that the unknown API is as expected.

+1
source share

Test setup and property retrieval must be performed in the context of testing an object’s business function. If there is no business function that checks a property, then you either don’t need this property, or you need more tests. I suggest you add properties by adding the tests they need, rather than adding them before writing any tests.

By testing a business function, you treat the code as something more out of a black box in terms of unit test. This allows you to switch implementation details from the bottom with much less impact on the tests. Since Refactor is an important (and often overlooked) step in the TDD cycle, this means that code editing is much less. You can also understand that (for example) a property should not have a public setter and should be assigned using a method that performs validation and other business logic.

+1
source share

Unit testing should be limited to the testing functionality that you implement in your application.

You should not check the API / SDK your application relies on. Partly because it's a waste of time (does your company want to pay for you to test the X-Third-Party SDK / API?), And partly because it introduces “noise” into your unit test package. The context of your unit test library should be to simply check only the code in your application.

0
source share

All Articles