When developing, I like to have my tests in the same folder as the code and make the build process excluded test code for a production build (<Compile Remove=”**\*Test.cs” />).
So I created a .NET Core command line application in Visual Studio Code (my.command.line.csproj) and placed a test project file inside the samen folder (my.command.line.test.csproj).
my.command.line.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Remove="**\*Test.cs" />
</ItemGroup>
</Project>
my.command.line.test.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<IsPackable>false</IsPackable>
<GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="1.3.2" />
<PackageReference Include="MSTest.TestFramework" Version="1.3.2" />
</ItemGroup>
</Project>
Then I executed dotnet test
This will throw the following error:
Program.cs(10,27): error CS0017: Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.
This can be fixed by adding: <GenerateProgramFile>false</GenerateProgramFile> to a PropertyGroup inside the my.command.line.test.csproj.
Then when you execute dotnet test again, you will get the error:
MSBUILD : error MSB1011: Specify which project or solution file to use because this folder contains more than one project or solution file.
To fix it, execute:
dotnet test “my.command.line.test.csproj”
When you execute dotnet watch test, you will first get the error
Multiple MSBuild project files found in ‘C:\Dev\’. Specify which to use with the –project option.
You might want to fix it like: dotnet watch –project “my.command.line.test.csproj” test, but then the watcher will start watching but you will get the error:
MSBUILD : error MSB1011: Specify which project or solution file to use because this folder contains more than one project or solution file.
watch : Exited with error code 1
watch : Waiting for a file to change before restarting dotnet…
To fix this, execute dotnet watch –project “my.command.line.test.csproj” test “my.command.line.test.csproj”
Now your tests will be executed and the tests will be re-executed, when any of the files change.