testfx icon indicating copy to clipboard operation
testfx copied to clipboard

TestContext information is not available inside TestMethod Attribute Extension Execute Method

Open anshuls01 opened this issue 7 years ago • 6 comments

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: tc

Expected behavior

Required Tfs Run id and Testcontext object inside Execute method

anshuls01 avatar Jan 30 '18 14:01 anshuls01

@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?

jayaranigarg avatar Feb 05 '18 14:02 jayaranigarg

Hello Jaya, Thanks for the response, We required few more information:

  1. RunBy
  2. Tfs_BuildNumber
  3. Tfs_BuildPlatform
  4. DeploymentDirectory
  5. FullyQualifiedTestClassName
  6. ResultsDirectory
  7. TestDir
  8. BuildDirectory
  9. ControllerName (not sure)
  10. Tfs_TeamProject
  11. 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

anshuls01 avatar Feb 07 '18 05:02 anshuls01

@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.

VanKrock avatar Feb 12 '18 14:02 VanKrock

@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.

luis-cortina avatar Feb 20 '19 19:02 luis-cortina

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.

Camerus avatar Mar 09 '19 01:03 Camerus

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.

avibitran avatar Mar 25 '20 21:03 avibitran