C# – DeploymentItem Attribute causes all tests in test class to fail with error “Unable to create instance of class [TestClass]”

cdeploymentitemmstestunit testing

I'm getting a strange error when I try to run a set of unit tests where the class has a DeploymentItem attribute.

The unit testing solution has 2 test classes, one to test business logic which I'll refer to as 'class a' (this one works fine regardless) and one to test reading of data from the DeploymentItem file which I'll refer to as 'class b'.

With the DeploymentItem attribute set ANYWHERE within the two test classes, the tests in class b all fail with the error…

Unable to create instance of class [class b]. Error: System.NullReferenceException: Object reference not set to an instance of an object..

However, if I completely remove the DeploymentItem attribute from both classes, all tests run, but obviously fail in class b because there is no file to read from, therefore all asserts fail.

I hope I've explained the problem fully, if not I should be able to clarify more if required.
Very grateful for any help.

Best Answer

A quick guess: Have you checked the Enable deployment checkbox in your .testrunconfig file? If not, DeploymentItemAttribute is not going to work.

Not sure whether it will give you that exception, though, but it's worth checking out.