Search code examples
c#xunit

Why can't I use 'dotnet run' with xUnit in C#?


I'm creating an application which requires unit tests. I'm using the .NET xUnit-framework.

First, I initialized a new "Hello, World! class" with

dotnet new console

Then I added the unit test

dotnet new xunit

When I run dotnet run or dotnet test, I get this error:

 error CS0017: Program has more than one entry point defined. Compile with /main to specify the type thatcontains the entry point.

I read about there being a Main defined in the xUnit which interferes with the entry point in Program, but how would I be able to have these separated? That is, how would I be able to run both dotnet run and dotnet test?


Solution

  • Found a solution to be able to run both dotnet test and dotnet run here.

    The problem is that a test project builds its own Program.cs file. You want to add following to your .csproj:

    <GenerateProgramFile>false</GenerateProgramFile>