How to prevent "over testing" in a test case? (C # / NUnit)

I am currently working on some test cases, and I regularly find that in each case I get a few statements. For example (more simplified and comments taken for brevity):

[Test] public void TestNamePropertyCorrectlySetOnInstantiation() { MyClass myInstance = new MyClass("Test name"); Assert.AreEqual("Test Name", myInstance.Name); } 

This seems acceptable in principle, but the point of the test is to verify that when the class instance is created with the given name, the Name property is set correctly, but it fails if something goes wrong when creating the instance, it goes to approval.

I reorganized it like this:

 [Test] public void TestNamePropertyCorrectlySetOnInstantiation() { MyClass myInstance; string namePropertyValue; Assert.DoesNotThrow(() => myInstance = new MyClass("Test name")); Assert.DoesNotThrow(() => namePropertyValue = myInstance.Name); Assert.AreEqual("Test Name", namePropertyValue); } 

but of course now I am actually testing three things here; In this test, I am not interested in checking whether the instance of the MyClass instance was successfully created or that the Name property was successfully read, it was checked in another case. But how can I verify the last statement without approving the first two first, given that it is not even possible to run the test if the first two are refused?

+7
c # unit-testing nunit
source share
4 answers

It’s just that you have other tests that check that an exception is thrown if you initialize it in an invalid way. The first form in this case is beautiful, IMO.

Personally, I would not depend on the dogma of "one statement per test." Try to test one logical way through the code in order to get granularity as accurately as possible, as it makes practical sense.

+12
source share

I really don't understand what you mean by over-testing IMO, over-testing is an attempt to try private methods.

I take my tests for code documentation. So, if I have several statements in the instruction, then there is a high probability that I will be able to rearrange the tested method into several smaller methods, or sometimes I split my test method into several different testing methods. Following the one-assert-per-test rule, you can have reasonable test method names, which in turn form the documentation for your code. The naming convention that I follow for test methods is the Name_scenario_expectation method (From RoyOsherove The Art of Unit Testing ). So, also thinks in terms of code documentation. Do, you think that having an affirmative will (in addition to checking expectations) will help you / another developer better understand the code, and then continue and write this statement. But again, always make sure that you have the correct test method names.

+1
source share

In your specific example, you do not need to claim that something is not throwing if its part of the test execution. This aspect has already been tested in your first test, which is more readable. If the constructor or getter property throws an exception, NUnit will terminate the test with the corresponding error message. TBH I'm not sure what the idea of ​​Assert.DoesNotThrow () is, because if you omit it, you will get almost the same result, but you should definitely not use it as part of the usual test execution. The whole point of having exceptions as part of the language is that you do not need to check for errors after each line of code.

0
source share

Grzenio is right about this. The first, simplest test example will fail if an exception is thrown - there is no need to explicitly verify this.

However, you must verify that an exception is thrown when invalid data is passed to it.

 [Test] public void TestInvalidNameThrowsException { MyClass myInstance; string namePropertyValue; Assert.Throws(() => myInstance = new MyClass(null)); Assert.Throws(() => myInstance = new MyClass("INVALID_NAME_125356232356")); } 

(for example, I don't know C # one bit, but you should get this idea.)

0
source share

All Articles