0

My scenario is that I do not want to depend on an environment's installed dotnet version. I would ideally want to have a standalone XUnit app using which I can run tests on the target environment.

.NET Core console applications have an OutputType as Exe and so after being published as standalone we could execute the published executable. This is understandable as the Console app has an entry point within the app. Where as in case of a XUnit test project we do not have an entry point.

Just adding <OutputType>Exe</OutputType> to an XUnit test project did not help (understandably)

Kiran
  • 56,921
  • 15
  • 176
  • 161

1 Answers1

0

I was able to achieve by doing the following:

  • In the XUnit test project that I would like to run, modify the .csproj to be like:

```

  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <OutputType>Exe</OutputType>
    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="xunit" Version="2.3.1" />
    <PackageReference Include="xunit.runner.utility" Version="2.3.1" />
  </ItemGroup>

</Project>

```

  • Include a Runner as mentioned here(Notice that this does have an entry point): https://github.com/xunit/samples.xunit/blob/master/TestRunner/Program.cs

  • Now publish the application as a standalone app. For example, I would like to run tests written in .NET Core on a Linux machine. Example: dotnet publish -c Release -r linux-x64

  • Now let's say if your test project was named as 'Foo.Tests', you should see an executable file with name 'Foo.Tests' in the published output. Execute it by doing './Foo.Tests'

Kiran
  • 56,921
  • 15
  • 176
  • 161
  • I found a similar simpler approach I posted an answer to in a similar question about running without the sdk installed: https://stackoverflow.com/a/64888158/2118268 I haven't tried this with a standalone app though. – csmacnz Nov 18 '20 at 08:04