Testing C # Blocks. Should you unit test something in a derived class that was taken care of in the base class?

public class Foo<T> { public Foo(Bar bar) { if (bar == null) throw new ArgumentNullException("bar"); } } public class Foo : Foo<Object> { public Foo(Bar bar) : base(bar) { } } 

Basically, I understand that I should unit test constructor for Generic Foo. Should I also unit test the constructor for the non-universal version? The reason I ask is because an exception is thrown at the constructor level ...

 [TestMethod] [ExpectedExeption(typeof(ArgumentNullException))] public void GenericFooNullArgumentInConstructor() { var foo = new Foo<int>(null); } //Is this test necessary? [TestMethod] [ExpectedExeption(typeof(ArgumentNullException))] public void NonGenericFooNullArgumentInConstructor() { var foo = new Foo(null); } 
+4
source share
4 answers

In a word: Yes. The key here is regression - you have to write unit tests to check your current implementation (or even before the implementation if you are TDD), but you also write it to protect yourself from breaking code changes in the future.
Since in the future both the base and the subclass can be changed, both should be tested.

+2
source

Yes. Despite all the implementations, the easiest way to verify that your derived classes have retained the guarantees of your base class. Especially when Bob the intern comes in later and changes your derived class to do something else.

+3
source

It depends...

why are you writing unit tests?

1) to check the code you are about to write

2) to check the code that you already wrote down

3) to check the code that someone can write in the future

If you are writing code to verify something that you are going to write, write it for the requirements of the code you are going to write.

If you write tests to test your code, check the code you write.

If your written tests do not allow other people to modify your code, it is not your responsibility.

Example

You have a base class A with a specific method, M. You have tested this method back, side and upside down. Then you create 3 subclasses of A and name them B, C and D. You add new functional methods for classes B and C.

You should check the new methods of class B and C because this code has not been verified, but the method M from class A has already been verified.

You redefine method M in class D, add code, and then call the base method M. The new override method should be tested and should be treated as a completely new method. unit test cannot confirm that the base class method M has been called.

Writing the same test 4 times does not help anyone. Every developer should rewrite tests for the code they write. If this code is tested, and the tests meet the requirements, and the tests pass, then the code is good.

Carrying out the same exam three times in a row is pointless.

On the other hand

We do not live in an ideal world, and if you have problems with other developers who do not test their own code, it may be useful for you to do the work for them. But understand, it’s not your job to check the code that they write is what their work is.

+1
source

You should also write unit tests for the derived class! Unit tests ensure that the class under test works as you expect. It gives you the opportunity to reorganize it someday. If then you make a mistake while reorganizing the constructor of the derived class - no unit test will find this error. (If you have not written unit tests for a derived class).

0
source

All Articles