Skip to content

When a class contains only tests with [Ignore], class cleanup runs but not class init #355

@dee-see

Description

@dee-see

Steps to reproduce

Run the following test class, only ClassCleanup runs and there is a NullReferenceException.

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace UnitTestProject1
{
	[TestClass]
	public class UnitTest1
	{
		private static object Obj;

		[ClassInitialize]
		public static void ClassInit( TestContext context )
		{
			Obj = new object();
		}

		[ClassCleanup]
		public static void ClassCleanup()
		{
			Console.WriteLine( Obj.GetType() );
		}

		[TestMethod]
		[Ignore] // The only test in this class is ignored
		public void TestMethod1()
		{
			Assert.AreEqual( 1, 1 );
		}
	}
}

Expected behavior

One of two scenarios

  1. The class is not even instantiated and nothing runs
  2. ClassInitialize and ClassCleanup run

Instantiating the class but running neither ClassInitialize nor ClassCleanup could leave some undisposed class variables... Though I'm wondering as I'm writing this if that's a bad practice and should be handled by an IDisposable test class.

Actual behavior

Only the class cleanup is executed, resulting in a NullReferenceException in this example.

My real life scenario is that we have a class with several tests and the only tests that matches the TestCaseFilter in this build is [Ignore]d.

Environment

VS 2017 15.5.5
MsTest v2 1.3.0-beta2, 1.2 and 1.1.18

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedUp for grabs; can be claimed by commenting.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions