Solved

using MS Test in class library project

Posted on 2016-08-24
2
42 Views
Last Modified: 2016-08-25
Hi,

I create a TestFramework project and two additional unit test projects.
I want to create a test framework and let two unit test projects use a bunch of helpers classes in a test framework project.
However, one of classes in a test framework contains a method that always has to run first before all other unit tests run.
So I wanted to use [AssemblyInitializer] fixture in this class in a test framework, but even if I add 'using Microsoft.VisualStudio.TestTools.UnitTesting;' directive, I can add any fixtures.

Is there any way I can add MS test fixture on a class in a class library project?

3.JPG
0
Comment
Question by:Isabell
2 Comments
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 41768762
I doubt that this will do what you want anyway. According to the documentation:

Identifies a method that contains code to be used before all tests in the assembly have run and to allocate resources obtained by the assembly. This class cannot be inherited.

Your assembly, in this case, would be the TestFramework project. Each other project in your solution will be its own assembly.

What I would suggest is to put your common code into a function that is in the shared library, and have methods defined within your tests, tagged with the AssemblyInitializer attribute, that call out to the shared code. (This shared code is added as a reference to each of your unit test projects.) You cannot inherit the AssemblyInitializer attribute, so I think this would be the best you can get.

e.g.

Shared Code
#define DEBUG

namespace ClassLibrary1
{
    public class Class1
    {
        public void SharedMethod()
        {
            System.Diagnostics.Debug.WriteLine($"{nameof(Class1)}.{nameof(SharedMethod)} running...");
        }
    }
}

Open in new window


Unit Test Project 1
#define DEBUG

using ClassLibrary1;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [AssemblyInitialize]
        public static void Initialize(TestContext context)
        {
            Class1 instance = new Class1();

            instance.SharedMethod();
        }

        [TestMethod]
        public void TestMethod1()
        {
            System.Diagnostics.Debug.WriteLine($"{nameof(UnitTest1)}.{nameof(TestMethod1)} running...");
        }
    }
}

Open in new window


Unit Test Project 2
#define DEBUG

using ClassLibrary1;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject2
{
    [TestClass]
    public class UnitTest2
    {
        private static TestContext ctx;

        [AssemblyInitialize]
        public static void Initialize(TestContext context)
        {
            Class1 instance = new Class1();

            ctx = context;
            instance.SharedMethod();
        }

        [TestMethod]
        public void TestMethod2()
        {
            System.Diagnostics.Debug.WriteLine($"{nameof(UnitTest2)}.{nameof(TestMethod2)} running...");
        }
    }
}

Open in new window


Screenshot
0
 

Author Comment

by:Isabell
ID: 41771154
Good idea! Thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video discusses moving either the default database or any database to a new volume.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now