Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssemblyInitialize method doesnt run before tests

I am using MsTest V2 framewrok for my tests. I have Test automation framework (TAF) project and project with tests. Tests project inherited from TAF and contains only tests. In TAF i have a class which contains method which should run before all tests but it doesnt work at all. By the way BeforeTest method works fine.

public class TestBase
{
    [AssemblyInitialize]
    public static void BeforeClass(TestContext tc)
    {
        Console.WriteLine("Before all tests");
    }
    [TestInitialize]
    public void BeforeTest()
    {
        Console.WriteLine("Before each test");
    }
}


[TestClass]
public class FirstTest : TestBase
{
    [TestMethod]
    public void FailedTest()
    {
        Assert.IsTrue(false,"ASDASDASD");
    }
}

If I put "AssemblyInitialize" method to tests projects then it work.

What am I doing wrong?

like image 576
TheX Avatar asked May 24 '17 08:05

TheX


People also ask

What is AssemblyInitialize?

The method decorated by [AssemblyInitialize] is called once before running the tests of the assembly. The method decorated by [AssemblyCleanup] is called after all tests of the assembly are executed. These methods can be located in any class as long as the class is decorated by [TestClass] .

How do I test a method in Visual Studio?

In the Visual Studio editor, set a breakpoint in one or more test methods that you want to debug. Because test methods can run in any order, set breakpoints in all the test methods that you want to debug. In Test Explorer, select the test methods and then choose Debug Selected Tests from the shortcut menu.

How do I ignore a test in Visual Studio?

When you add the [Ignore] attribute, the test will be ignored by the test runner. It will show up in Test Explorer with a warning icon and will count as Skipped.


1 Answers

Just put [TestClass] onto your TestBase:

[TestClass]
public class TestBase
{
    [AssemblyInitialize]
    public static void BeforeClass(TestContext tc)
    {
       Console.WriteLine("Before all tests");
    }

    [TestInitialize]
    public void BeforeTest()
    {
           Console.WriteLine("Before each test");
    }
}
like image 54
Francesco Bonizzi Avatar answered Oct 28 '22 20:10

Francesco Bonizzi