testfx icon indicating copy to clipboard operation
testfx copied to clipboard

Class Cleanup gets called immediately after a single test case using VSTestConsoleWrapper

Open agadekar opened this issue 2 years ago • 9 comments

Describe the bug

I am using VsTestConsoleWrapper api to run tests. I have added same test method twice using below code -

Collection<TestCase> cases = new Collection<TestCase>();
  cases.Add(testCase);
  cases.Add(testCase);
var  consoleWrapper = new VsTestConsoleWrapper(runnerLocation, new ConsoleParameters { LogFilePath = logFilePath });
consoleWrapper.RunTests(cases, defaultRunSettings, handler);

However the ClassCleanup method gets called immediately after the first time test method is called and does not wait for the second instance to finish. Is this expected?

Expected behavior

class initialize  started at 3/9/2023 7:19:28 PM
test initialize  started at 3/9/2023 7:19:28 PM
TestA started at 3/9/2023 7:19:28 PM
TestA ended at 3/9/2023 7:20:18 PM
test cleanup  started at 3/9/2023 7:20:18 PM
class cleanup  started at 3/9/2023 **7:21:09** PM
test initialize  started at 3/9/2023 7:20:19 PM
TestA started at 3/9/2023 7:20:19 PM
TestA ended at 3/9/2023 7:21:09 PM
test cleanup  started at 3/9/2023 7:21:09 PM

Actual behavior

Logs from Test Run

test initialize  started at 3/9/2023 7:19:28 PM
TestA started at 3/9/2023 7:19:28 PM
TestA ended at 3/9/2023 7:20:18 PM
test cleanup  started at 3/9/2023 7:20:18 PM
class cleanup  started at 3/9/2023 7:20:19 PM
test initialize  started at 3/9/2023 7:20:19 PM
TestA started at 3/9/2023 7:20:19 PM
TestA ended at 3/9/2023 7:21:09 PM
test cleanup  started at 3/9/2023 7:21:09 PM

Additional context

Note how Class Cleanup was called at 7.20.19 before second run of TestA finished (7.21.09). Shouldn't it be called after 7.21.09?

agadekar avatar Mar 09 '23 20:03 agadekar

Also, logs added using https://learn.microsoft.com/en-us/windows-hardware/drivers/taef/wexlogger do not get captured in TestResultMessage. Is there a way to do that?

agadekar avatar Mar 10 '23 16:03 agadekar

Hi @agadekar,

I tried this in small repo with mstest v3.0.2 and the `classcleanup' is called after the two methods.

image image (1)

so please can you check what is the mstest version in your project? and if it's the latest can you send sample project? or use --diag:log.txt and send the logs file to us? if the logs has sensitive data you can add a feedback ticket in VS and then modify it by sending the logs as privet

engyebrahim avatar Mar 13 '23 14:03 engyebrahim

SampleProject.zip

we are using 3.0.2v of mstest

agadekar avatar Mar 13 '23 15:03 agadekar

Thanks @agadekar,

in here the problem when happens when we run the same test twice from a class has only one test and that's because in the code we check if all tests run then call the class cleanup which happens after first call to the test.

engyebrahim avatar Mar 13 '23 16:03 engyebrahim

can we please add it as a feature to recognize duplicate test cases in a single run as individual test cases and run class cleanup only after all these test cases have finished running?

agadekar avatar Mar 13 '23 18:03 agadekar

@engyebrahim @Evangelink I am not sure why this was moved from testfx for vstest, this looks like a problem of testfx rather than the platform.

The best solution I can think of on platform side is to select distinct tests based on IDs, but that will impose the behavior on all frameworks (admittedly we do similar thing with sources where we select unique sources).

nohwnd avatar Aug 05 '24 10:08 nohwnd

@Youssef1313 this should probably be addressed on mstest side, do you see that as feasible? Rather asking here before we play another round of ping-pong with this issue.

nohwnd avatar Jun 12 '25 14:06 nohwnd

Moving to MSTest for now. I'll investigate.

Youssef1313 avatar Jun 12 '25 14:06 Youssef1313

I remember investigating this, the code that runs assembly initialize and assembly cleanup is imho using hashtable to keep the tests to run, but we provide 2 tests so after the first test it will remove the single instance, and run asssembly cleanup.

nohwnd avatar Jun 12 '25 16:06 nohwnd

This is likely already fixed (same fix/issue as https://github.com/microsoft/testfx/issues/3018). So I'm going to close this.

Youssef1313 avatar Jun 27 '25 10:06 Youssef1313