Solved

using MS Test in class library project

Posted on 2016-08-24
2
70 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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