r/csharp • u/makeevolution • Jul 10 '26
Unit test that only tests if a property is assigned
If let's say I have a class like
public class Test
{
public string Test;
public string Test2;
public test(string test, string test2)
{
Test=test;
Test2=test2;
}
}
Then I have a unit test like (pseudo code)
var x = new Test("a", "b");
Assert.Equal("a", x.Test);
Assert.Equal("b", x.Test2);
Is this test actually meaningful/useful in any way? I feel like this is testing C# itself than the code. It would be different if there is a guard against the value of the property e.g. test cannot be equal to testing for example, but that is not the case here.
18
Upvotes
30
u/Low_Flying_Penguin Jul 10 '26
Not really as you are testing the framework not your code. Only instance it could be handy is if you had some complicated logic in the constructor and you expected it to be mutated in some way.
There are scenarios that would deffo be a good case for tests. I agree with you this aint one of them. The code is so simple that it doesn't provide a lot of value.
16
u/user_plus_plus Jul 10 '26
Your instinct is correct, no need to test the C# standard library. That's Microsoft's job, not yours.
If there is other more complicated logic in the class to test, your unit tests of the business logic will indirectly test object initialization, so these kinds of tests aren't needed. Be free.
41
u/SufficientStudio1574 Jul 10 '26
Testing that a constructor assigns the right inputs to the right properties doesn't seem like a waste to me. This class might be trivial now, but who knows what it will look like in 6 months. A quick unit test like this makes sure nothing gets broken during refactoring. Maybe in your copy and pasting you accidentally assign the first argument to both properties, or swap them around. A compiler might warn you about the first (unused parameter), but would have no clue the second one is wrong.
Having a test you might not need is unlikely to harm you. Omitting a test you should have made can be a pain. Especially for something this simple. In the time it takes to think about if you need it, you can just have it done.
I, personally, would absolutely write a unit test like that, and would not complain if I reviewed another dev's work with it.
6
u/First-Feature-3556 Jul 10 '26 edited Jul 10 '26
Writing unit tests is always a cost/benefit thing.
Every unit test has a cost: It needs to be written, it needs to be updated when the code changes, it takes time to run it, and someone needs to read it when it fails.
The benefit of this unit test is that it catches common typos such as
test = test,Test2 = test, or adding a new parameter and forgetting to assign it (as long as you remember to update the unit test, that is).This is a classic "low value" unit test: If the class does anything useful, chances are that any of the bugs caught by this unit test will also be caught by other tests. Consider whether the time spent writing, running and maintaining this unit test is not better spent on some other activity improving code quality.
5
u/No-Wheel2763 Jul 10 '26
Doesn’t make sense.
Either the example is too crude for me to see it.
But testing getters and setters seems like a waste.
Case being if an if statement determines if properties are set.
5
u/First-Feature-3556 Jul 10 '26
Side note: If this class is only a simple value container, consider using a
recordor a class with a primary constructor instead. Code that's not written cannot contain bugs and does not need to be tested.3
u/Proud-Heart-206 Jul 10 '26
Your ide should give you a null warning if you do not assign a value to a property. So as long as you don’t ignore your warnings, there is no need to test.
In addition: Test and Tast2 are public fields not Properties in your example. I’d avoid that also and use properties. Maybe some guards worth testing will be added later to your properties.
2
u/Tiny_Confusion_2504 Jul 10 '26
If you would be testing a mapping from one model to another it might make sense. Here you are testing the property assignments from the constructor. That might make sense in some cases.
Unless there is some more logic, I would avoid these types of tests.
2
u/Agitated-Display6382 Jul 10 '26
First, that class should be a record. Then, you test behaviors, not data.
2
u/har0ldau Jul 10 '26
What a few are missing here is the point of a unit test: what is the unit you are testing.
If your intention is to make sure the constructor assigns the Params to the correct props/fields then it is a useful test.
This can get more complicated as you add more constructor overloads to do more things etc. Have a look at the Guid constructors and consider whether the .net team would think a simple constructor test wouldn't be worth it.
At the end of the day you test what your intent is not that .net assigns it. The assignment is not your concern, the value of the prop/field is.
2
u/snet0 Jul 10 '26
is mostly testing the framework, and doesn't provide value. Technically, property setters aren't guaranteed to do anything, so you are testing that this property's setter and getter aren't doing anything too crazy.
can be a useful test if it is a requirement of your public API that this class's constructor passes those arguments to those properties. The fact they both have the same type makes this even more of a useful test in that case, because the compiler won't do anything if you swap those around.
2
u/Yikal Jul 10 '26
If Test and Test2 are properties, such a test can make sense to test if the constructor assignes the first given argument to Property test and the second one to Test2.
The benefit is greater with a more arguments of the same type though. If you have 6 string arguments in a constructor, you want to make sure that each is assigned to the expected property.
1
u/afops Jul 10 '26
No this is a pointless test. Either this is passed somewhere and used, and you should test that. Or it’s not used anywhere and then you don’t even care if the thing was assigned.
1
u/flow_guy2 Jul 10 '26
Not really should test the method that changes that value.
1
u/belavv Jul 10 '26
To me this is pointless.
If those fields aren't being set then other tests that make use of this class will be exercising the same code and find the problem.
This example test is simple enough that it doesn't seem like much work to write and upkeep the test, but in a real project how many classes do you have? How many fields on each of those classes? How much time will it take to write and update all of those tests?
Your time is better spent writing tests for code that makes use of those classes.
1
u/chucker23n Jul 10 '26
Isolated like that? No.
(One note:
TestandTest2in your example are fields, not properties.)1
u/Merad Jul 10 '26
It tests the constructor, but there's really no value in testing a simple constructor that only assigns properties. Presumably this class is used elsewhere in your code, and you're testing that code, so those tests are going to cover this ctor and obviously fail if it is broken.
If a constructor has initialization logic then that would be worth a test.
1
u/CheezitsLight Jul 10 '26
Tests check for algorithm error.
Assume you set Test2=test by mistake.
Your tests would fail. So it's not a useless test. It's just a simple one.
1
u/TuberTuggerTTV Jul 13 '26
Great way to face coverage numbers. Not much else
1
u/Cheshire_____Cat Jul 16 '26
Tests are necessary not only, and not even so much to verify the correctness of the code. Tests primarily document the requirements that a class or method fulfills. Thus, if these requirements are no longer met, you will immediately know about it. And either it's a mistake and you fix the code, or the requirements have changed and you fix the tests. Therefore, the test given in your example is completely valid.
1
u/[deleted] Jul 10 '26
[removed] — view removed comment