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?
c # unit-testing nunit
Flynn1179
source share