?
Solved

using MS Test in class library project

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

Setting up LaraDock for Laravel

Learn how to set up LaraDock in a Laravel project - LaraDock gives us an easy way to run a Laravel application using Docker in a single command.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

800 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