Reputation: 101
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:
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:
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)
Upvotes: 10
Views: 1496
Reputation: 906
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.
Upvotes: 7
Reputation: 3694
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
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..
Upvotes: 0
Reputation: 170
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?
Upvotes: 0
Reputation: 13681
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.
Upvotes: 0