Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of the TestExecution class in MSTest?

I found the TestExecution class, which exposes a bunch of events that would be incredibly helpful. However, I can't find a reference to it other than MSDN's documentation, and the documentation doesn't provide any hints as far as to how it is used, or how I might get a reference to an instance during a test run.

Looking with .NET Reflector wasn't helpful either - TestExecution is implemented by one other class, UnitTestExecution, which is internal. I could not find any other references to either class throughout any of the Microsoft.VisualStudio.QualityTools.* libraries, neither in the form of further implementations nor references as property types in any other classes.

How might I gain access to these events?

like image 872
Daniel Schaffer Avatar asked Dec 15 '11 21:12

Daniel Schaffer


1 Answers

An TestExecution instance is provided to you when implementing a custom TestExtensionExecution. It provides an Initialize method, which you can override to subscribe to the test events.

This is usually part of an implementation of a custom test attribute in MSTest.

Edit To create your own test extension, start with creating a new attribute that derives from Microsoft.VisualStudio.TestTools.UnitTesting.TestClassExtensionAttribute, which is an abstract class requiring you to provide a TestExtensionExecution via the GetExtension() method.

Apply your attribute to one of your test methods and you should be able to subscribe to those events during the test (inside your TestExtensionExecution implementation returned by the attribute)

Note that you also have to implement a ITestMethodInvoker which you should be able to create from the TetMethodInvokerContext.TestMethodInfo property supplied to the TestExtensionExecute.CreateTestMethodInvoker method.

like image 123
J. Tihon Avatar answered Oct 24 '22 06:10

J. Tihon