6

I have a library with unittest helper classes. I reference NUnit in this project but now Visual Studio thinks this project is a test project and complains that there are no tests discovered. This project is not meant to be a test project, just a regular .net standard library that references NUnit.

I want to "convince" visual studio that this is NOT a test project. Any idea's?

Technical details: the project is a .Net Standard 2.0 project and I reference the following NuGet packages:

  • Moq 4.10.0
  • Moq.AutoMock 1.2.0.111
  • NUnit 3.10.1

I use Visual Studio 2017 Enterprise version 15.8.4

An example of what this library is for:

  public abstract class AutoMockerTestBase
  {
     protected AutoMocker AutoMocker { get; private set; }

     [SetUp]
     public virtual void SetUp()
     {
        AutoMocker = new AutoMocker(MockBehavior.Strict);
     }

     [TearDown]
     public virtual void TearDown()
     {
        AutoMocker.VerifyAll();
     }
  }

To reproduce the problem:

  • Add a .net standard2.0 class library project to a solution.
  • Reference NUnit

Now notice the visual studio changes the project icon to a test project.

Help me find a way to have visual studio NOT think this project is a test project, and make sure that vs does not try to discover tests in this project.

EDIT: Looking through the "Tests" output I found this:

[16/09/2018 10:12:55 Warning] Exception NUnit.Engine.NUnitEngineException, Exception thrown discovering tests in C:\Users\me\Source\Repos\Shared\src\UnitTestHelpers\bin\Debug\netstandard2.0\Shared.UnitTestHelpers.dll
[16/09/2018 10:12:55 Warning] An exception occurred in the driver while loading tests.
[16/09/2018 10:12:55 Warning]    at NUnit.Engine.Runners.DirectTestRunner.LoadDriver(IFrameworkDriver driver, String testFile, TestPackage subPackage)
   at NUnit.Engine.Runners.DirectTestRunner.LoadPackage()
   at NUnit.Engine.Runners.TestDomainRunner.LoadPackage()
   at NUnit.Engine.Runners.DirectTestRunner.EnsurePackageIsLoaded()
   at NUnit.Engine.Runners.DirectTestRunner.Explore(TestFilter filter)
   at NUnit.Engine.Runners.MasterTestRunner.Explore(TestFilter filter)
   at NUnit.VisualStudio.TestAdapter.NUnit3TestDiscoverer.DiscoverTests(IEnumerable`1 sources, IDiscoveryContext discoveryContext, IMessageLogger messageLogger, ITestCaseDiscoverySink discoverySink) in D:\repos\nunit\nunit3-vs-adapter\src\NUnitTestAdapter\NUnit3TestDiscoverer.cs:line 90
[16/09/2018 10:12:55 Warning] Innerexception: System.IO.FileNotFoundException: Could not load file or assembly 'nunit.framework' or one of its dependencies. The system cannot find the file specified.
File name: 'nunit.framework'
   at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
   at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo)
   at System.AppDomain.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
   at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
   at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
   at NUnit.Engine.Drivers.NUnit3FrameworkDriver.CreateObject(String typeName, Object[] args)
   at NUnit.Engine.Drivers.NUnit3FrameworkDriver.Load(String testAssemblyPath, IDictionary`2 settings)
   at NUnit.Engine.Runners.DirectTestRunner.LoadDriver(IFrameworkDriver driver, String testFile, TestPackage subPackage)
  • You 2 projects in your solution? – Llazar Sep 15 '18 at 14:39
  • The solution has several other projects, some of them are indeed test projects. – Michiel Dulfer Sep 15 '18 at 14:44
  • You need to provide more information. You wanted to create a test project or something else? – Llazar Sep 15 '18 at 14:47
  • No I do NOT want to create a test project. This should be a regular .net standard 2.0 library that happens to reference NUnit. It contains no tests but it is referenced by test projects (the project is called "UnittestHelpers") – Michiel Dulfer Sep 15 '18 at 14:55
  • I edited the post with added clarification – Michiel Dulfer Sep 15 '18 at 14:58
  • You need to check if you have circular reference between projects in your solutions. – Llazar Sep 15 '18 at 15:02
  • I checked. This project references only non-unittest projects, and it is referenced only by unittest projects. The project builds just fine and everything works, but the icon shown in the solution explorer is that of a unittest project, and when i run unittests in the solution, I see warnings that no tests are discovered in the "UnittestHelper" project. I want VS to stop treating this project like a test project because it isn't. – Michiel Dulfer Sep 15 '18 at 15:16
  • Add reference to NUnit 3 Test Adapter by using NuGet – Nisfan Sep 15 '18 at 15:18
  • @Nisfan I want the exact opposite. This project is NOT a unittest project. Adding a reference to a test adapter would only make it look MORE like a test project ;) – Michiel Dulfer Sep 15 '18 at 15:21
  • You cannot. Visual Studio simply verified reference list to see if `nunit.framework` i s in there. Get used to that. – Lex Li Sep 15 '18 at 16:10
  • That is very... unsatisfactory :( I'll add a dummy test for now to stop VS from complaining... – Michiel Dulfer Sep 15 '18 at 16:46
  • I think that there's a `` property (PropertyGroup) you can add/set to false in your csproj files. At least it seems the case from reading some github issues related to the different test packages. Perhaps that will help – pinkfloydx33 Sep 15 '18 at 18:51
  • The `` fixed the exception. Thank you! Visual Studio still uses the TestProject icon though. It's just visual so I'll just ignore that ;) If you post your suggestion as an answer, I can mark it as such! – Michiel Dulfer Sep 16 '18 at 08:27
  • I give up! After a clean and rebuild the error is back :( – Michiel Dulfer Sep 16 '18 at 08:33
  • I stumbled on this today. It's been 5 years. Still no fix? – Lionet Chen Aug 13 '23 at 12:14

4 Answers4

3

Inside the .csproj file you should be able to add the following:

<PropertyGroup>
  <IsTestProject>false<IsTestProject>
</PropertyGroup>

This has worked for me with libraries referencing XUnit that aren't test libraries and it should work for NUnit as well.

humbleice
  • 816
  • 8
  • 15
0

If you apply NonTestAssemblyAttribute at the assembly level, that will tell NUnit that it isn't a test assembly. You'll have to see whether that prevents the error when running under Visual Studio, however.

Charlie
  • 12,928
  • 1
  • 27
  • 31
0

Have you checked if you have the following entry in your project file:

<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

It defines the project as a test project (old style).

See also here: How to convert an existing assembly to a ms unit test assembly?

Mathias Rotter
  • 101
  • 1
  • 4
0

What worked for me is adding a new intermediary library project that references NUnit and the main library project only references the intermediary library and not NUnit directly.

For example:

Solution
  \
   ----- MainLibrary
  \
   ----- IntermediaryLibrary
            \
             ------ NUnit

  • MainLibrary has a reference to IntermediaryLibrary.
  • IntermediaryLibraryhas the NUnit package installed.

The icon for MainLibrary will be the normal library icon. The icon for the IntermediaryLibrary will be the unit test icon.

You can still have classes in MainLibrary that reference NUnit and create helper types for unit testing.

In my scenario we have attributes that we wanted to define in a shared library without treating that shared library like a test library..

quip
  • 3,666
  • 3
  • 32
  • 45
  • Thank you for your suggestion, it sounds like it might work, but it would probably result in an extra DLL having to be shipped with the helper DLL. I no longer do development work and don't have VS 2017 installed, so unfortunatly I am unable to check if your reply would solve the problem, but thank you for your input and effort! – Michiel Dulfer Aug 06 '22 at 10:12