Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running multiple Unit Tests in an unit test project

I am currently working on a project using C# and Visual Studio 2012.

I have a hard time trying to figure out why VS 2012 does not want to run all of my tests.

I have one test projects which runs 1 unit test out of 2. No matter what I've tried, it does not want to run the other unit test. It is not detected via the Test Explorer, the "Not Run" test does not include the file.

Here's a basic framework to put it clearly:

(I have edited the framework to include every part of the solution)

Solution
    MainProject
        ProjectA.Tests (that is the project)
           UnitTestsA.cs (Actually run)
           UnitTestsB.cs (Neven run)

Here's the code for the second unit test file:

using System; 
using Microsoft.VisualStudio.TestTools.UnitTesting; 
namespace MagicAdmin.Tests 
{      
    [TestClass] 
    public class UnitTest1 
    { 
        [TestMethod] 
        public void TestMethod1() 
        { } 
    } 
} 

How can I solve this? What do I do wrong?

like image 684
hsim Avatar asked Oct 21 '25 07:10

hsim


2 Answers

I was having this problem, but just figured out a solution.

I am building a .NET Core library for use in a DotNet console app. There is a separate solution for the library. The library contains projects for the library, a unit test project, and a showcase project.

I followed the steps in these articles:

  • https://learn.microsoft.com/en-us/dotnet/core/tutorials/library-with-visual-studio
  • https://learn.microsoft.com/en-us/dotnet/core/tutorials/testing-library-with-visual-studio?tabs=csharp
  • https://learn.microsoft.com/en-us/dotnet/core/tutorials/consuming-library-with-visual-studio?tabs=csharp

I added some extra library classes in the library, and I made sure to change the package version for the lbirary. I rebuilt the library, and published it to NuGET.

I added an extra class in the test project, and could not get the routine in the test class to run. I had followed these steps: - Added a regular C# class to the test project - Added [TestClass] at the top of the class - Added a public void method, and added [TestMethod] above it - Used manage NuGET projects on the test project, and made sure I updated to the latest version of the library package

But "Run All" was not seeing the extra test.

I finally discovered that the new testing class needed to be declared as public! Then the next Run All properly picked up the extra test method in the new test class.

Here is an outline of the necessary things in the extra testing class:

using System;
using UtilityLib.APIDir;

namespace UtilityLibTest
{
   [TestClass]
   public class UnitTest_NewAPI
   {
       [TestMethod]
       public void TestNewAPI()
       {
          NewAPI tester = new NewAPI();
          bool someResult = tester.DoSomething();
       }
   }
}

Other comments on Visual Studio projects:

There are several issues with Visual Studio, where shutting down all copies of Visual Studio, and restarting, can fix problems.

When I created my first .Net core lib project, published it to NuGET, and then used NuGET to load it into another project, but I was unable to reference the classes within the library. I started looking into some tools to try to look into the NuGET package.

But after system restart (For an entirely unrelated issue), I restarted Visual Studio, and then I was able to access the library classes from my main project.

Another issue is from a project that consumes a nuget library, sometimes after updating the version of the assembly with nuget, the calling program might not be able to access the namespaces or classes in the updated library. A rebuild all on the calling project fixed that problem for me yesterday.

like image 162
Garnet R. Chaney Avatar answered Oct 23 '25 19:10

Garnet R. Chaney


In order for Visual Studio to recognize your unit tests, you need to rebuild the unit test project. It is possible that the unit test project is not set to build as part of your current build configuration and thus is not being built when you build the solution.

like image 20
John Koerner Avatar answered Oct 23 '25 21:10

John Koerner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!