10

I converted a vNext format .NET xUnit test project (with project.json) to the new .csproj format in Visual Studio 2017 RC and started getting the below error. Most of the online answers to this error say "You have two Main methods; get rid of one." This seems like an obvious solution, but this project has only one Main method.

Error:

CS0017 Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point. Project.Name C:\path\to\Program.cs

Program.cs:

using XunitProgram = Xunit.Runner.DotNet.Program;

namespace My.Namespace.Tests
{
    public static class Program
    {
        public static void Main(string[] args)
        {
            XunitProgram.Main(args);
        }
    }
}

Old project.json:

{
  "version": "1.0.0-*",
  "testRunner": "xunit",
    "buildOptions": {
        "emitEntryPoint": true,
        "preserveCompilationContext": true,
        "debugType": "full"
    },

    "dependencies": {
        "dotnet-test-xunit": "2.2.0",
        "xunit": "2.2.0",
        "Microsoft.DotNet.InternalAbstractions": "1.0.0"
    },
  "frameworks": {
    "net462": {}
  }
}

New Project.csproj:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net462</TargetFramework>
        <RuntimeIdentifier>win7-x86</RuntimeIdentifier>
        <DebugType>full</DebugType>
        <PreserveCompilationContext>true</PreserveCompilationContext>
        <AssemblyName>My.Project.Tests</AssemblyName>
        <OutputType>Exe</OutputType>
        <PackageId>My.Project.Tests</PackageId>
        <GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
        <GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
        <GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
        <GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
        <RootNamespace>My.Project.Tests</RootNamespace>
        <StartupObject />
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0-preview-20170106-08" />
        <PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
        <PackageReference Include="xunit" Version="2.2.0" />
    </ItemGroup>

    <ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
        <Reference Include="System" />
        <Reference Include="Microsoft.CSharp" />
    </ItemGroup>

    <ItemGroup>
        <Folder Include="SampleInput\" />
    </ItemGroup>

</Project>
J.D. Mallen
  • 4,339
  • 3
  • 22
  • 33

4 Answers4

11

This took a bit of tinkering to figure out. Upon migration to the new project format in VS 2017, Microsoft quietly added a dependency for Microsoft.NET.Test.Sdk, which I believe has its own Main method.

If you create a new xUnit Test Project in VS 2017 RC alongside the one that you had migrated, you will notice that it no longer creates a Program.cs with a Main method invoking the XUnit runner.

To fix this, delete your sole visible Main method. Your tests will still execute as normal, provided you have the above package references (Microsoft.NET.Test.Sdk, xunit.runner.visualstudio, xunit).

J.D. Mallen
  • 4,339
  • 3
  • 22
  • 33
  • 1
    Thanks for this. I have a project using MSTest that I just opened in VS2017 and migrated to csproj that displayed the same problem. – Nick Mar 11 '17 at 11:45
  • 1
    Thank you! I have xunit tests baked into my web app for the internal stuff, and got this with no reasonable explanation until now. – Yablargo May 09 '17 at 15:23
11

For when you have a console application with tests in it I prefer this:

tl;dr; Add <GenerateProgramFile>false</GenerateProgramFile> inside a <PropertyGroup> element in your test project's .csproj file.

From:

https://andrewlock.net/fixing-the-error-program-has-more-than-one-entry-point-defined-for-console-apps-containing-xunit-tests/

Dave Mateer
  • 6,588
  • 15
  • 76
  • 125
  • +1 ~ I like this. It more or less says exactly what I surmised 10 months earlier, that it injects its own Main method / Program.cs, though I'm happy to see someone went into more detail to find out how/why and come up with an elegant solution that doesn't require trashing one's own entry point. If I create another test project using this, I'll probably follow your approach. Thanks. – J.D. Mallen Sep 06 '18 at 18:20
  • For future reference - in my case it seemed not to work, but i added false inside my main project, not test project, and it helped. – Piotr P Jan 25 '20 at 17:13
0

None of my xunit projects (type netcoreapp) has a main method. The main method is provided by xunit. Your Program.cs was not necessary with project.json in the first place. Now, with csproj, this seems promoted to error.

See this original xunit documentation and search for main.

Thomas
  • 5,080
  • 27
  • 42
  • still this message should not occure. Shouldn't every console project be able to contain xunit tests and a main method? – MushyPeas Mar 14 '17 at 15:16
  • 1
    No because a project reflects a software module (assembly in .NET). Unit Tests are a development artifact similar to e.g. build files. They should not be deployed to a production environment since they do not fulfill a purpose there usually but introduce risk. – Thomas Mar 14 '17 at 20:12
0

I had a similar issue. Check if you have the AddCommandLine(args) configuration option added to your ConfigurationBuilder.. e.g.

var config = new ConfigurationBuilder()
            .AddCommandLine(args)
            .AddEnvironmentVariables(prefix: "ASPNETCORE_")
            .Build();

Remove the entry. This was causing my issue...

Hope it helps