Reputation: 5718
I am getting the below warning in VS Code while writing Unit Tests with NUnit:
The type 'MyClass' in 'MyProject.cs' conflicts with the imported type 'MyClass' in 'MyProject, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'Myproject\Tests\MyClass.cs'. (CS0436) [Tests]
These are the steps I took so far:
Navigated to my desired folder for all C# projects (e.g. E:\MyCSharpProjects
).
Created a new C# Console Project via dotnet new console -o MyProject
.
Wrote some code.
Ran the following command once: dotnet new -i NUnit3.DotNetNew.Template
. I assume this installs NUnit project templates.
Created a new folder inside MyProject
's root folder called Tests
.
Navigated inside the new Tests
folder and ran the following commands:
dotnet new nunit
dotnet add reference ../MyProject.csproj
Installed the NUnit
package via NuGet on MyProject.csproj
.
Wrote some test code.
NOTE 1: For both MyProject.csproj
and Tests.csproj
, I set TargetFramework
to netcoreapp2.2
because I do not want to use 3.0 just yet.
NOTE 2: To avoid build errors, I stop Assembly Information generation by adding the following entries under PropertyGroup
to both my .csproj files:
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
NOTE 3: Below is my MyProject.csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NUnit" Version="*"/>
</ItemGroup>
</Project>
NOTE 4: Below is my Tests.csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="nunit" Version="*" />
<PackageReference Include="NUnit3TestAdapter" Version="*" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="*" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StudentService.csproj" />
</ItemGroup>
</Project>
The warning started showing up once I tried referencing a class inside my Tests
project called MyClass
. This class is only declared inside my Tests
project and does not have any name conflicts with MyProject
. I have however read that this might be caused by Tests.csproj
referencing MyProject.csproj
while being located inside MyProject
.
My C# project is structured as follows:
E:\MyCSharpProjects
MyProject
MyProject.csproj
<MyProject code files>
Tests
Tests.csproj
<Tests code files>
My questions are:
Is having a test project that references my actual project the only way to use NUnit?
Is there a way to only have 1 C# project that has NUnit tests inside it, similar to Java and JUnit?
I am keeping my test project inside my actual project so they are always together. Is this the standard practice? And if not, what is?
How do I get rid of said warning? I'm don't want to ignore it because I don't like having warnings in my code.
UPDATE: In case my questions are too broad to answer, a link on standards on how to write NUnit tests from beginning to end would also be useful.
SUMMARY: desmondgc's answer worked! Turns out all I needed to do was add a .sln
file to my project and everything worked perfectly! No warnings, and no need to disable Assembly Information generation at all!
Many thanks to everyone who helped!
Upvotes: 0
Views: 271
Reputation: 614
Please see Unit testing C# with NUnit and .NET Core from the .NET Core Guide. They propose a directory structure like this:
/unit-testing-using-nunit
unit-testing-using-nunit.sln
/PrimeService
Source Files
PrimeService.csproj
/PrimeService.Tests
Test Source Files
PrimeService.Tests.csproj
This is generally what I've used on all my projects, as well.
Regarding the warning, it sounds like you've defined the same class and namespace in both projects. The namespace should usually mirror the directory structure, so if you have a project called MyProject, a test project called MyProject.Tests and a class called MyClass in each, I would expect to find the fully-qualified names MyProject.MyClass
and MyProject.Tests.MyClass
. Even better, call the test class MyClassTests
or something like this.
Upvotes: 1