0

In TFS 2013 when running tests from MTM, we were able to get the ID of the currently running test case by using the TestContext Properties dictionary as follows:

var testCaseID = TestContext.Properties["__Tfs_TestCaseId__"].ToString();

This statement returns NULL if tests are being run as part of TFS vNext build (BDT).

In order to update some fields in test results for instance Comments/Notes, I will need to know the ID of the current test run and test case. How can I return that information?

I am using TFS 2017 on-premise.

Thanks!

maryama
  • 1
  • 2
  • https://stackoverflow.com/questions/17995330/how-to-retrieve-the-id-of-the-workitem-that-started-an-automated-test?rq=1 – Prof. Falken Apr 24 '19 at 09:38

2 Answers2

0

You can’t get test case id in test method as running test through MTM when run tests during the build (Functional test).

The Workaround is that you can specify the value in runsettings and get the value during test.

More information, you can refer to Supplying Run Time Parameters to Tests

starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53
  • Thank you for your suggestion. First, I tried to use runsettings before for Coded UI tests but I kept getting "NullReferenceException" . I made sure the setting file is selected and still got the exception. Is this not supported for Coded UI? Also, there is no way to add a dynamic value like Test Run ID to as a runsetting value? – maryama Mar 21 '17 at 16:34
  • @maryama Based on my test, The Coded UI Test can't get the value in runsettings through TestContext, you can read the file to get the value in your test method. Cannot find the way to dynamic get the test case or test run id. What's the purpose that you need to get test case id? – starian chen-MSFT Mar 22 '17 at 03:11
  • I run my Coded UI tests via TFS vNext build. I want to write some notes back to the "Comments" section of each test case result at the end of the run. Back when running my tests via MTM, I used to do that by returning the current "test run" and "test case" Ids and use the TFS test management API to write my notes back to the test case result comment section. Is there any way to do that when running tests through vNext build? – maryama Mar 22 '17 at 16:08
  • @starianchen-MSFT is there a list of parameters and their string literals that I can pass in through the release run time parameters? – Reed Jul 13 '18 at 14:44
0

It is possible to get the Test Case Identifier while test is running in TestMethod. You can get it by querying TFS using TFS API. You can do that from your C# code by finding the test results object for current running test case run. You will get the current test run ID from the testContext.

user1466508
  • 139
  • 1
  • 1
  • 7