locked
Problem with Unit Test project RRS feed

  • Question

  • User-881099725 posted

    I have a solution in which I have my target assembly (contains the classes to be tested), another project with unit tests as well. In my target assembly I only expose classes that are meant to be public, the rest are marked internal. I need to be able to write unit tests for my internal classes, for that reason I added the InternalsVisibleTo("UnitTests").

    In my unit test project I have created a base class that is decorated with the unit test attributes (TestClass, ClassInitialize, etc.). Then each of the unit test classes inherits from the base unit test class. Both my test class and the unit test base class are marked public (if I make them internal I get an error indicating that the attribute can only be applied to public classes).

    Now, my unit test base class is parametized (templated), so it takes a generic parameter which is actually the type of the target class (the class being tested). The problem is that when the target class is INTERNAL I get an error during compilation.

    "UTA004: Illegal use of attribute on... The TestMethodAttribute can be defined only inside a class marked with the TestClass attribute."

    Strange fact is that the class that contains the methods that cause the error DOES have the TestClass attribute. What is wrong there? I come from a NUnit background so these are actually my first trials with VS Unit Tests.

    Tuesday, January 24, 2012 11:16 PM

Answers

  • User-434868552 posted

    @ degt

    please study this thread:  http://xunit.codeplex.com/discussions/204365 
      "testing "private" revisted ~~ what is xUnit.net approach?"

    Jim Newkirk and Brad Wilson are the creators of xUnit.net.  http://xunit.codeplex.com/ Jim is Microsoft's manager of the codeplex.com website; Brad Wilson is a member of the ASP.NET MVC team.

    "xUnit.net is a unit testing tool for the .NET Framework. Written by the original inventor of NUnit, xUnit.net is the latest technology for unit testing C#, F#, VB.NET and other .NET languages."

    I need to be able to write unit tests for my internal classes, for that reason I added the InternalsVisibleTo("UnitTests").

    Really?  Why?  imho, generally, you want anything not public to be a black box ... if your class responds all green to your unit tests, why care about what's inside the black box?  the danger of knowing to much about your black box is that you will write brittle, unmaintainable tests.

    Do you really want to fall into the code coverage trap?  I spent four months comparing xUnit.net to MSTest; my research involved programmatically analyzing millions of lines of code.

    Jim Newkirk:  "we will never put anything in xUnit.net that makes testing private methods or fields simpler or easier. I have been down this road with NUnit before. "

    g. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 25, 2012 11:47 PM