Solved

using MS Test in class library project

Posted on 2016-08-24
2
83 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

729 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