microsoft / testfx-docs

Docs for MSTest V2 test framework
Other
103 stars 49 forks source link

How to invoke MsTestV2 unit test programmatically #90

Closed saiparth closed 3 years ago

saiparth commented 3 years ago

I am using specflow with MsTestV2. Due to unavoidable reasons, I would like to trigger this unit test programmatically. Where i can find details for this?

Haplois commented 3 years ago

What do you mean? Can you explain it a bit more, and maybe provide us an example?

saiparth commented 3 years ago

https://social.msdn.microsoft.com/Forums/vstudio/en-US/ccd08ce1-a86b-4965-af68-1db8cd7d6715/how-to-execute-test-methods-programmatically-using-c-may-be-using-reflection?forum=vsunittest I am trying to the same with MS test V2

Haplois commented 3 years ago

If you like preserve the entire test platform you can execute the test via https://github.com/microsoft/vstest/blob/master/src/Microsoft.TestPlatform.VsTestConsole.TranslationLayer/VsTestConsoleWrapper.cs class. A sample usage is in our integration tests. (https://github.com/microsoft/vstest/blob/master/test/Microsoft.TestPlatform.TestUtilities/IntegrationTestBase.cs)

saiparth commented 3 years ago

I have kept all test name in a xml, will read that as a list and invoke one by one. So I dint use console runner.

saiparth commented 3 years ago

@Haplois https://github.com/microsoft/testfx/blob/master/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/MSTestExecutorTests.cs Can this be used?Is there any other example? Since its using mock I am not able to write test properly.

Haplois commented 3 years ago

@saiparth I have kept all test name in a xml, will read that as a list and invoke one by one. So I dint use console runner.

VsTestConsoleWrapper has a method to run tests one by one as well.

@Haplois https://github.com/microsoft/testfx/blob/master/test/UnitTests/MSTest.CoreAdapter.Unit.Tests/MSTestExecutorTests.cs Can this be used?Is there any other example? Since its using mock I am not able to write test properly.

No it cannot, it's mocking the actual process, tests might not be run properly. Please check our E2E tests; this example might be useful, be we recommend to use VsTestConsoleWrapper.