hsim
hsim

Reputation: 2090

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?

Upvotes: 4

Views: 7947

Answers (4)

HO LI Pin
HO LI Pin

Reputation: 1691

if you just add 2nd C# class file to manage your 2nd set of test code in single unit test project, make sure those things happen.

  1. Decorate it with the right test keyword attributes to tell Visual studio this is a test class , method , example....

"MS unit test" , test class attribute = [TestClass] , test method attribute = [TestMethod]

"xUnit" , test method attribute = [Fact]

  1. Make sure class , method is set as public

something like this shall help enter image description here

Upvotes: 0

Rain Yu
Rain Yu

Reputation: 1

I finally figured out my problem. I think most people encounter this problem same as me. The root problem is that I add the unit test class in the wrong way. Try to re-add the file using the "Test" section. This would make the class get dependency to the project. Otherwise, the project did not include it when running test.

Upvotes: 0

Garnet R. Chaney
Garnet R. Chaney

Reputation: 81

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:

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.

Upvotes: 7

John Koerner
John Koerner

Reputation: 38077

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.

Upvotes: 2

Related Questions