Visual Studio 2022 not running XUnit tests
Asked Answered
D

6

6

I've created a EntityFramework ASP.NET solution and i'm trying to create a XUnit test project to test my differents classes i've created.

I've created a TestClass for my Activity Class :

using LADS_Model;
using LADS_WebUI.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using Xunit;
using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;

namespace LADS_XUnit
{
    public class UnitTest_Activity
    {
        [TestClass]
        public class ActivityController
        {
            private List<Activity> GetTestActivities()
            {
                var testActivities = new List<Activity>();
                testActivities.Add(new Activity { Id = 1, Name = "Chaussure" });
                testActivities.Add(new Activity { Id = 2, Name = "Crevettes" });
                testActivities.Add(new Activity { Id = 3, Name = "Sandwich" });
                return testActivities;
            }

            [TestMethod]
            public void GetAllActivities_ShouldReturnAllActivities()
            {
                var testActivities = GetTestActivities();
                var controller = new ActivityController();

                var result = controller.GetTestActivities();
                Assert.Equals(testActivities.Count, result.Count);
            }
        }


    }
}

The problem is that when I launch my testClass, I do have the Test showing up in the test Explorer but VS tells me that the test did not execute and I have no idea why because it's not showing any errors or any messages to explain why it didnt execute

capture of my test view

Output of Tests : Output Tests

Drona answered 24/3, 2022 at 14:27 Comment(5)
Open the Output window (under the View menu), then select "Show output from: Tests". See what the error is.Baldwin
@Baldwin i've edited my message with the Output from TestsDrona
Wait, you said xUnit right? [TestMethod] and [TestClass] aren't xUnit annotations. xUnit uses [Fact] on the test method, and nothing on the classBaldwin
Do not nest classesProvencal
XUnit doesn't have anything like [TestClass], any public class that has public void methods marked as [Fact] should be discovered and run. I'm not sure if it will handle nested classes.Baton
B
6

Problem 1: xUnit vs. MSTest Attributes


I see you have this:

using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;

...so I assume you're porting existing MSTest code to xUnit, in which case rather than changing the attributes in your code-base, you could alias MSTest's attributes to xUnit:

using TestClassAttribute  = SomeDummyAttribute;
using TestMethodAttribute = Xunit.FactAttribute;

internal sealed class SomeDummyAttribute : Attribute {}

If you're using C# 10.0 or later you can use global using which will be shared by all source-files in the same project.

Also, consider using Shouldly or Fluent Assertions instead of the Assert/Asserts classes.

Problem 2: The xunit.runner.visualstudio NuGet package is dependent on your Visual Studio and .NET versions.

The xunit.runner.visualstudio package is rather finnicky about the specific versions of Visual Studio and .NET that you're using - unfortunately neither NuGet nor Visual Studio shows any message that updating xunit.runner.visualstudio to a version that drops support for your environment (In my case, I was targeting .NET Core 3.1 but updated to xunit.runner.visualstudio 2.5.0 (which requires .NET 6.0+), and doing that made VS' Test Explorer just fail silently instead of listing the test-cases).

I've compiled the following table: make sure that your combination of Visual Studio and your target .NET platform/version is supported:

xunit.runner.visualstudio Release date xUnit Visual Studio .NET Fx .NET (.NET Core) UWP Notes
2.5.1 to 2.5.7 2023-07-06 1.9.2+ VS2022 (v17.0+) 4.6.2+ 6.0+
2.5.0 2023-07-06 1.9.2+, 2.0+ VS2019 (v16.10+) 4.6.2+ 6.0+ Last version to officially support VS2019 (theoretically, it should still work VS2019 but don't expect bugfixes).
2.4.5 2022-05-05 1.9.2+, 2.0+ VS2019 (v16.8+) 4.7.2+ 3.1+ 10.0.16299+ Last version to support .NET Core 3.1 and UWP.
2.4.2 to 2.4.3 2020-06-02 1.9.2+, 2.0+ VS2017 (v15.9+) 2.0+ 2.1+ 10.0.16299+ Last version to support Visual Studio 2017 and .NET Framework 2.0.
2.4.0 to 2.4.1 2018-07-17 1.9.2+, 2.0+ VS2012+ 2.0+ 1.0+ 10.0+ Last version to support Visual Studio 2012, Visual Studio 2013, and Visual Studio 2015.
2.2.0 to 2.3.1 2017-02-20 1.9.2+, 2.0+ VS2012+ 2.0+ 1.0+ 10.0+
2.1.0 2015-09-27 1.9.2+, 2.0+ VS2012+ 2.0+ 8.1, 10.0+ Last version to support "Windows Store Apps" v8.1, Windows Phone 8.1, and Portable Class Library (Profile259).

Note: A restart of Visual Studio may be required when changing the xunit.runner.visualstudio NuGet package version from one that is incompatible to one that is compatible.

Biota answered 24/3, 2022 at 14:47 Comment(0)
W
18

I had a different issue with pretty much the exact same symptoms. In my case I was simply missing the nuget package xunit.runner.visualstudio which I had removed during some nuget troubleshooting.

It worked again immediately after installing it through the nuget GUI in Visual Studio.

Weep answered 6/3, 2023 at 13:25 Comment(2)
thank you, first time trying unit tests with existing repo. But refactoring layers. The new layers are missing some packages. again, thank you! I was so confused.Mendes
Solved my problem. No idea, why I had to reinstall the runner ... yesterday everything was working perfectly, reboot -> runner missing. Thanks a lot.Atalaya
B
6

Problem 1: xUnit vs. MSTest Attributes


I see you have this:

using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;

...so I assume you're porting existing MSTest code to xUnit, in which case rather than changing the attributes in your code-base, you could alias MSTest's attributes to xUnit:

using TestClassAttribute  = SomeDummyAttribute;
using TestMethodAttribute = Xunit.FactAttribute;

internal sealed class SomeDummyAttribute : Attribute {}

If you're using C# 10.0 or later you can use global using which will be shared by all source-files in the same project.

Also, consider using Shouldly or Fluent Assertions instead of the Assert/Asserts classes.

Problem 2: The xunit.runner.visualstudio NuGet package is dependent on your Visual Studio and .NET versions.

The xunit.runner.visualstudio package is rather finnicky about the specific versions of Visual Studio and .NET that you're using - unfortunately neither NuGet nor Visual Studio shows any message that updating xunit.runner.visualstudio to a version that drops support for your environment (In my case, I was targeting .NET Core 3.1 but updated to xunit.runner.visualstudio 2.5.0 (which requires .NET 6.0+), and doing that made VS' Test Explorer just fail silently instead of listing the test-cases).

I've compiled the following table: make sure that your combination of Visual Studio and your target .NET platform/version is supported:

xunit.runner.visualstudio Release date xUnit Visual Studio .NET Fx .NET (.NET Core) UWP Notes
2.5.1 to 2.5.7 2023-07-06 1.9.2+ VS2022 (v17.0+) 4.6.2+ 6.0+
2.5.0 2023-07-06 1.9.2+, 2.0+ VS2019 (v16.10+) 4.6.2+ 6.0+ Last version to officially support VS2019 (theoretically, it should still work VS2019 but don't expect bugfixes).
2.4.5 2022-05-05 1.9.2+, 2.0+ VS2019 (v16.8+) 4.7.2+ 3.1+ 10.0.16299+ Last version to support .NET Core 3.1 and UWP.
2.4.2 to 2.4.3 2020-06-02 1.9.2+, 2.0+ VS2017 (v15.9+) 2.0+ 2.1+ 10.0.16299+ Last version to support Visual Studio 2017 and .NET Framework 2.0.
2.4.0 to 2.4.1 2018-07-17 1.9.2+, 2.0+ VS2012+ 2.0+ 1.0+ 10.0+ Last version to support Visual Studio 2012, Visual Studio 2013, and Visual Studio 2015.
2.2.0 to 2.3.1 2017-02-20 1.9.2+, 2.0+ VS2012+ 2.0+ 1.0+ 10.0+
2.1.0 2015-09-27 1.9.2+, 2.0+ VS2012+ 2.0+ 8.1, 10.0+ Last version to support "Windows Store Apps" v8.1, Windows Phone 8.1, and Portable Class Library (Profile259).

Note: A restart of Visual Studio may be required when changing the xunit.runner.visualstudio NuGet package version from one that is incompatible to one that is compatible.

Biota answered 24/3, 2022 at 14:47 Comment(0)
B
5

In my case, i was missing the nuget package Microsoft.NET.Test.Sdk. (you also need to install the nuget package xunit.runner.visualstudio.)

Bikini answered 20/9, 2023 at 20:8 Comment(0)
A
0

These were what I needed

 <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
 <PackageReference Include="Microsoft.TestPlatform" Version="17.7.2" />
 <PackageReference Include="Moq" Version="4.20.69" />
 <PackageReference Include="xunit" Version="2.5.1" />
 <PackageReference Include="xunit.abstractions" Version="2.0.3" />
 <PackageReference Include="xunit.runner.console" Version="2.5.1">
Acquiescence answered 7/10, 2023 at 8:31 Comment(1)
For my net8.0 framework project, I did not need Microsoft.TestPlatform, and I'm not currently using Moq, but the others were needed.Fechner
H
0

In my case, my whole solution was in .net core 5.0, so was the test project but xunit.runner.visualstudio works only from 6.0. I changed only the test project to 6.0 and it worked.

Heterophony answered 2/2 at 13:20 Comment(0)
C
0

In my case, this combination of packages is not working with Test Explorer in Visual Studio 2022 17.9.0. If I downgrade xunit.runner.visualstudio to version 2.5.6 or if I remove FluentAssertions, then Test Explorer will run the tests. This is with a .NET 8 project.

  <PackageReference Include="coverlet.collector" Version="6.0.1"/>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7"/>
Certification answered 5/3 at 17:18 Comment(1)
The table in the accepted answer tells the same thing.Bozuwa

© 2022 - 2024 — McMap. All rights reserved.