UnitTesting properties in .Net?

I am working on a lib that I want to release in open source. I started writing tests for code, and I was wondering how I can test a property in a .Net object. Suppose I have the following:

public class Person{ #region variables private string _name = String.Empty; private string _surname = String.Empty; #region properties public string Name{ get{ return _name; } } public string Surname{ get{ return _surname; } set{ _surname = value; } } } 

I have two code related questions:

  • How do I Unit test a property that only has a getter (as the name in the example)
  • How do I Unit test the property with the installer and the receiver (for example, last name in the example)

I want to check properties that are so simple because I already found errors in another code, because Itellinsense did the wrong auto-completion, and the property did not return the correct variable.

Update:

I'm not talking about simple properties, as in the example, they have some kind of logic and it is rather difficult to debug. Writing a test that uses a setter to test getters, and vice versa, is not very good, because if there is a failure, I will not know which method to blame. I use properties because they were added as public variables, and then additional logic was added.
+1
source share
9 answers

Like a unit test property that just has a getter (e.g. name in example)

Really not so different from testing if you have a setter. you just need to find another way to determine the output. May be in ctor or as a result of other settings / operations on the object.

 [Test] public void NamePropTest() { Person p = new Person(); //Some code here that will set up the Person object // so that you know what the name will be Assert.AreEqual("some known value...", p.Name); } 

If we had setters for Name and SurName, but only for getter for FullName, the test might look like this:

 [Test] public void NamePropTest() { Person p = new Person(); p.Name = "Sean"; p.Surname = "Penn"; Assert.AreEqual("Sean Penn", p.FullName); } 
+8
source

Do not waste time writing silly tests for getters and setters.

Another test will probably set the Name, and then get this property so that you will have code coverage for the getter.

You should check for anything public, including properties. If you do not test the property, you risk that someone may add some logic to it, violating the functionality.

Also, you should not rely on the fact that it is tested in other tests. This makes your tests fragile and makes it difficult to determine where a problem, like a test, will test more than one thing.

+9
source

You must check the properties. Also automatic properties!

Unittests assure that changes in the program do not interfere with the program.

You can end up changing the implementation of the property at some time, and you want to make sure that the program is still working properly. You do this with your tests.

Even if you use automatic properties (as a replacement for fields / member variables), the reason for creating them is that you will want to subsequently change their implementation. Then you want the tests to be there.

EDIT: (In response to the comment on shahkalpesh ...)

If you change implementations, tests may also require changes. So, I do not know why someone should test the simple ones to get / install?

Starting from this class:

 public class TimeOfDay { public int Hour{get; private set;} public int Minute{get; private set;} public TimeOfDay(int hour, int minute) { Hour = hour; Minute = minute; } } 

When the implementation changes, the tests remain valid!

 public class TimeOfDay { public int _minutesSinceMidnight = 0; public int Hour { get { return _minutesSinceMidnight / 60; } set { _minutesSinceMidnight = value * 60 + Minutes; } } public int Minute { get { return _minutesSinceMidnight % 60; } set { _minutesSinceMidnight = Hour * 60 + value; } } public TimeOfDay(int hour, int minute) { Hour = hour; Minute = minute; } } 

Throw some arithmetic functions of time and time or something else, and I would like the tests to show that it still works ...

+5
source

I think you should test them if you write them the way you do. In the end, you can ruin something.

Just something like

 var person = New Person(); person.Surname = "test"; Assert.AreEqual("test", person.Surname); 

After all TDD and n unit tests generally help to avoid most of the mistakes you can make.

If by chance you wrote it.

 public class Person{ #region variables private string _name = String.Empty; private string _surname = String.Empty; #region properties public string Name{ get{ return _name; } } public string Surname{ get{ return _name; } set{ _name = value; } } } 

then you will have a mistake.

Testing automatic properties is perhaps less valuable. But this is another question.

+3
source

These properties should be there because you have ways to use them. As for them, there should be unit tests, you should already have coverage for them. If a script is not needed, they probably should not be there at all.

+1
source

As I understand it, you should not test properties (i.e. those that are simple get / set).

I am not sure which version of C # you are using. But you can use automatic properties to avoid the simple installation / retrieval problems you encounter.

See link

+1
source

Do not waste time writing silly tests for getters and setters.

Another test will probably set the name and then get this property, so you will have code coverage for the recipient.

+1
source

My question is more about HOW write test. I thought about it, and I need to access my personal values ​​to make sure that the properties work properly, otherwise I don’t know how to do it. https://stackoverflow.com/users/59332/mandel

Good ... because you insist on knowing how to do it ....

 [Test] TestMethod() { Person p = new Person(); p.Name = "a name"; p.Surname = "a surname"; Assert.That(p.Name, Is.EqualTo("a name")); Assert.That(p.Surname, Is.EqualTo("a surname")); } 

However, this only works if you have setters ....

If you have only recipients, there are only two ways in which I can think that you can do this.

  • find out the return value in advance and confirm it.
  • Use Reflection to set the value and then validate that known value.

The best advice would be to give up and test something worthwhile, which actually adds value to your software. Testing getters and setters is an absolute waste of time unless something complicated happens after them ... which rarely happens.

+1
source

I think you meant that your properer only for getter uses private fields or other personal data. If you need to install them, the only way is to get them using Reflection (see All (something) information classes in System.Reflection). But there is tough discussion if this is good practice.

+1
source

All Articles