Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why isn't TestInitialize getting called automatically?

I'm using Microsoft.VisualStudio.TestTools.UnitTesting; but the method I marked as [TestInitialize] isn't getting called before the test. I've never used this particular testing framework before but in every other framework there is always a way of registering a Setup and TearDown method that will auto run before and after every single test. Is this not the case with the visual studio testing tools unit testing framework?

[TestClass]
public class RepoTest
{
    private const string TestConnectionString = @"Server=localhost\SQL2014EXPRESS64; Database=RepoTest; Trusted_Connection=True;";
    private const string MasterConnectionString = @"Server=localhost\SQL2014EXPRESS64; Database=master; Trusted_Connection=True;";

    [TestInitialize]
    private void Initialize()
    {
        using(var connection = new SqlConnection(MasterConnectionString))
        using(var command = new SqlCommand(Resources.Initialize, connection))
        {
            command.ExecuteNonQuery();
        }
    }

    [TestCleanup]
    private void Cleanup()
    {
        using (var connection = new SqlConnection(MasterConnectionString))
        using (var command = new SqlCommand(Resources.Cleanup, connection))
        {
            command.ExecuteNonQuery();
        }
    }

    [TestMethod]
    public void CreateARepo()
    {
        var repo = new Repo(TestConnectionString);
    }
}
like image 297
Daniel Robinson Avatar asked Jul 06 '14 22:07

Daniel Robinson


People also ask

Does TestInitialize run for each test?

TestInitialize and TestCleanup are ran before and after each test, this is to ensure that no tests are coupled. If you want to run methods before and after ALL tests, decorate relevant methods with the ClassInitialize and ClassCleanup attributes.

What is TestInitialize C#?

TestInitialize. This attribute is needed when we want to run a function before execution of a test. For example we want to run the same test 5 times and want to set some property value before running each time. In this scenario we can define one function and decorate the function with a TestInitialize attribute.

What is TestContext C#?

Each NUnit test runs in an execution context, which includes information about the environment as well as the test itself. The TestContext class allows tests to access certain information about the execution context.

What is ClassInitialize?

ClassInitialize runs only on the initialization of the class where the attribute is declared. In other words it won't run for every class. Just for the class that contains the ClassInitialize method. If you want a method that will run once before all tests or classes' initialization use the AssemblyInitialize .


1 Answers

Make Initialize and Cleanup public. You can also check, that at msdn all examples have public accessor.

In order to reproduce, make such test class:

[TestClass]
public class Tests
{
    [TestInitialize]
    public void Initialize()
    {
        Console.WriteLine("initialize");
    }

    [TestCleanup]
    public void Cleanup()
    {
        Console.WriteLine("cleanup");
    }

    [TestMethod]
    public void Test()
    {
        Console.WriteLine("test body");
    }
}

That test will produce the following results:

enter image description here

Making Initialize and Cleanup private, you'll see only test body being printed to the console:

enter image description here

Used Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly as unit testing framework version 10.1.0.0 and ReSharper 8.2 as a test runner.

like image 98
Ilya Ivanov Avatar answered Sep 30 '22 13:09

Ilya Ivanov