I would like to integrate OpenCover for XUnit test coverage to my Visual Studio project. I made a little example project just for testing the integration.
My example solution OpenCoverProject has two projects.
One XUnit project UnitTests with a ProgramUT.cs file:
public class ProgramUT
{
[Theory]
[InlineData(0, 0, 0)]
[InlineData(12, 10, 2)]
[InlineData(-6, -2, -4)]
public void AddTest(int _expected, int _x, int _y)
{
int actual = MyMathClass.Add(_x, _y);
Assert.Equal(_expected, actual);
}
}
And the main project Learning with a Program.cs file:
class Program
{
static void Main(string[] args)
{
}
}
public class MyMathClass
{
public static int Add(int x, int y)
{
return x + y;
}
}
I also have a script file which executes OpenCover and ReportGenerator. 00 is just a placeholder for some more path string.
set OpenCoverConsolePath=C:\Users\00\Apps\OpenCover\OpenCover.Console.exe
set XUnitConsolePath=C:\Users\00\packages\xunit.runner.console\2.4.1\tools\net461\xunit.console.exe
set LearningExePath=C:\Users\00\OpenCoverProject\bin\Debug\netcoreapp3.1\Learning.exe
set ReportGeneratorPath=C:\Users\00\ReportGenerator\netcoreapp3.0\ReportGenerator.exe
"%OpenCoverConsolePath%" ^
-target:"%XUnitConsolePath%" ^
-targetargs:"%LearningExePath%" ^
-output:"CodeCoverageResult.xml" ^
-register:user
"%ReportGeneratorPath%" ^
-reports:"CodeCoverageResult.xml" ^
-targetdir:"Report" ^
-sourcedirs:"Report"
start Report\index.htm
pause
When I execute the script file I get following error:
System.InvalidOperationException: Unknown test framework: could not find xunit.dll (v1) or xunit.execution.*.dll (v2) in C:\Users\00\OpenCoverProject\bin\Debug\netcoreapp3.1
I expect OpenCover to analyze that every method (it's just Add in our case) gets tested by the xunit tests. I don't know how I can manage this. I suppose the target should be xunit.console.exe and has to link to my Learning project Learning.exe somehow by the targetargs.
It would be great if someone has a hint or a solution for this problem.