Troubleshooting "Program Does Not Contain a Static 'Main' Method" When It Clearly Does...

Troubleshooting program does not contain a static 'Main' method when it clearly does...?

Are the properties on the file set to Compile?

Program does not contain a static ‘Main’ method suitable for an entry point

Create a .NET Class Library project if you only want a library project. If this is a project that already exists, you can set the Project Output type to a DLL ("Class Library") instead of an Executable ("Windows Application"/"Console Application") in the project properties.

Program does not contain a static 'Main' method suitable for an entry point In .Net MAUI Xunit

You need to make a few modifications to make xUnit work. I have a sample repository here: https://github.com/jfversluis/MauiUnitTestSample

All the modifications are in the csproj files of both projects and marked with a comment that starts with xUnit.

  1. Add net6.0 as a target in your .NET MAUI project.
<!-- xUnit: Add net6.0; here -->
<TargetFrameworks>net6.0;net6.0-android;net6.0-ios;net6.0-maccatalyst</TargetFrameworks>

  1. Make the OutputType of the .NET MAUI project exclude the net6.0 target
<!-- xUnit: The condition here only excludes this for the unit test project -->
<OutputType Condition="'$(TargetFramework)' != 'net6.0'">Exe</OutputType>

  1. In your xUnit project add UseMaui if you need to reference .NET MAUI APIs
<!-- xUnit: Add UseMaui if you need access to .NET MAUI APIs-->
<UseMaui>true</UseMaui>

You should now be able to add and run unit tests!

CS5001 Program does not contain a static 'Main' method suitable for an entry point XUnit

You need to create a separate project for your unit tests. And do not declare a class with Main method inside this project, xUnit has already a Main method that it uses to run the tests.

And make sure that the "Discover tests in real time from C# ...." is checked under Tools > Options > Test > General
Sample Image

Otherwise, you will need to click on "Run All" to discover all your tests, each time you add a new one.



Related Topics



Leave a reply



Submit