Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Visual Studio 2022 not running XUnit tests

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

capture of my test view

Output of Tests :
Output Tests

>Solution :

  • You’re using the wrong method attributes for xUnit, so the xUnit Runner won’t discover your tests:
  • It looks like you’re porting existing MSTest code to xUnit…
    • ..as you have using Assert = Microsoft.VisualStudio.TestTools.UnitTesting.Assert;
    • So you could add these as well:
      • using TestClassAttribute = SomeDummyAttribute;
      • using TestMethodAttribute = FactAttribute;
  • Also, consider using Shouldly or Fluent Assertions instead of the Assert/Asserts classes.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading