?
Solved

using MS Test in class library project

Posted on 2016-08-24
2
Medium Priority
?
128 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 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Integration Management Part 2
Screencast - Getting to Know the Pipeline
Suggested Courses

840 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