Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assembly & Class Cleanup should be called even if Initialize fails #695

Closed
andrewphamvk opened this issue Mar 3, 2020 · 1 comment · Fixed by #696
Closed

Assembly & Class Cleanup should be called even if Initialize fails #695

andrewphamvk opened this issue Mar 3, 2020 · 1 comment · Fixed by #696
Assignees
Milestone

Comments

@andrewphamvk
Copy link
Contributor

andrewphamvk commented Mar 3, 2020

Description

AssemblyCleanup/ClassCleanup method is not called if AssemblyInitialize/ClassInitialize method fails.
It would make sense to have the ability to cleanup partially initialized tests runs just like TestInitialize/TestCleanup.

See below comment regarding previous decision to break compatibility with MSTestV1 and support execution of TestCleanup even after TestInitialize failure.

@arnonax : Thanks for making this explicit. This was a conscious compat break to give unit test writers a choice to cleanup partially initialized methods. However in order to make the transition from v1 extremely smooth, it does make sense to have some setting to preserve compat.

/cc: @harshjain2 , @pvlakshm

Originally posted by @AbhitejJohn in #250 (comment)

Steps to reproduce

AssemblyInitialize/AssemblyCleanup

[AssemblyInitialize]
public static void AssemblyInitialize(TestContext context)
{
    File.AppendAllLines(@"C:\MSTest.txt", new [] { "AssemblyInit." });
    throw new Exception();
}

[TestMethod]
public void TestMethod1()
{
}

[AssemblyCleanup]
public static void AssemblyCleanup()
{
    File.AppendAllLines(@"C:\MSTest.txt", new [] { "AssemblyCleanup." });
}

Output

AssemblyInit.

ClassInitialize/ClassCleanup

[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
    File.AppendAllLines(@"S:\MSTest.txt", new[] { "ClassInit." });
    throw new Exception();
}

[TestMethod]
public void TestMethod1()
{
}

[ClassCleanup]
public static void ClassCleanup()
{
    File.AppendAllLines(@"S:\MSTest.txt", new[] { "ClassCleanup." });
}

Output

ClassInit.

TestInitialize/TestCleanup

[TestInitialize]
public static void TestInitialize(TestContext context)
{
    File.AppendAllLines(@"S:\MSTest.txt", new[] { "TestInit." });
    throw new Exception();
}

[TestMethod]
public void TestMethod1()
{
}

[TestCleanup]
public void TestCleanup()
{
    File.AppendAllLines(@"S:\MSTest.txt", new[] { "TestCleanup." });
}

Output

TestInit.
TestCleanup.

Expected behavior

The AssemblyCleanup and ClassCleanup methods should have a chance to run.

Actual behavior

The initialization methods throw and their corresponding cleanup method is not run.

@andrewphamvk andrewphamvk changed the title AssemblyCleanup should be called even if AssemblyInitialize fails Assembly & Class Cleanup should be called even if Initialize fails Mar 3, 2020
@nohwnd nohwnd added this to the 16.7.0 milestone Mar 24, 2020
@nohwnd
Copy link
Member

nohwnd commented Mar 24, 2020

@andrewphamvk makes sense, adding for consideration to the next sprint. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants