testfx
testfx copied to clipboard
TestContext information is not available inside TestMethod Attribute Extension Execute Method
Description
TestExtension Execute method accepts only one parameter of type ITestMethod during debug, if do quick watch on testMethod "((Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter.Execution.TestMethodInfo)testMethod).TestMethodOptions.TestContext" shows the testcontext information, but same not able to retrieve inside Execute method, because of permission level, we tried to get the values through runtime properties, still not able to resolve because of abstraction.
the requirement is to know the tfs test run id, inside Execute method.
just for work Around We tried to create a public property of testcontext type in test class, and try to access the values in Execute method using reflection, still not able to get. testMethodAssembly.GetType(TestClassName).GetProperty("Testcontext") the above line gives the property value, to GetValue required object of TestContext type, and that is not possible due to TestContext is a Abstract class, if use new Object then Target Mismatch Exception occurred.
Steps to reproduce
Implement TestMethod Attribute Extension inside Execute method not possible to get testContext, please find the attachment below:
Expected behavior
Required Tfs Run id and Testcontext object inside Execute method
@anshuls01 : We have not exposed TestContext as part of ITestMethod interface. Could you tell us what information do you need from this testcontext object apart from tfs run id?
Hello Jaya, Thanks for the response, We required few more information:
- RunBy
- Tfs_BuildNumber
- Tfs_BuildPlatform
- DeploymentDirectory
- FullyQualifiedTestClassName
- ResultsDirectory
- TestDir
- BuildDirectory
- ControllerName (not sure)
- Tfs_TeamProject
- Tfs_TestPlanId
I think, most of the other information we can get after querying the TFS database, by using the run id. in TFS TestResult.tbl_TestResult and dbo.tbl_TestRun contains all the information we required.
Thanks Anshul
@jayaranigarg It's really to make instance of test class as part of ITestMethod? For example object TestClassInstance? It's well be cool for initializing test class by test method attributes.
@cltshivash @pvlakshm I also want to add or edit a Property in the test context to influence some behaviour in my test not related to test data. Is there any other way to do this? Looks like if we cast (testMethod as TestMethodInfo).TestMethodOptions.TestContext will give us what's needed, but TestMethodInfo is internal. Please help as this is blocking me from releasing a product.
Just wanted to pile on here. Either exposing the TestContext or the test class instance (or both) to the ITestMethod interface would be incredibly valuable as it is not currently possible to use the parameters passed to the test method as part of shared test fixture setup in the TestInitialize method. I would like to either set them directly on the class or pass them in via the TestContext. As an added bonus, doing it this way would prevent me from having to change the signature of every single test method in my codebase.
To add to the why we need the TestContext. if I'm are using Data Driven test using DataSourceAttribute. The values of every iteration is inserted to the TestContext DataRow property, so if I want to determine the iteration number and I inserted it as part of the DataRow, I need the TestContext to get it.
