none
How to generate a .trx file for each test RRS feed

  • Question

  • Hello,

    I would like to know is there any possibility of generating a .trx file for each test separately?

    The problem is, I have some hundreds of tests and running with vstest. After completing the test run for all tests then only getting the .trx file. I have to wait for the complete test test run to get the summary. But I would like to get it in between. Is it possible?

    or

    I hope that before writing to the .trx file, the results related to the test run might be stored in some repository based on the test run id and finally the details are retrieved and then written on the .trx file once the test run gets completed. Please correct me if i am wrong.

    If this is true then how it can be queried from the repository to get the results before the .trx file gets generated? 

    Thanks & Regards,

    Anil


    Tuesday, July 23, 2019 7:03 AM

All replies

  • Hi R Anil,

    Welcome to MSDN forum.

    For your requirement, I'm afraid that the answer is negative. As far as I know, all test results are automatically integrated into one trx file.

    We recommend you could create a extra log file to log individual test result. please refer more: how to log

    If you want to run the single test which needs to run again. Please follow sample command:

    vstest.console.exe C:\Users\Admin\source\repos\UnitTestProject1\UnitTestProject1\bin\Debug\UnitTestProject1.dll /logger:trx;LogFileName=test.trx /Tests:TestMethod2

    If there are several tests,

    /Tests:TestMethod1,TestMethod2 ...

    Any feedback will be expected.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Wednesday, July 24, 2019 2:47 AM
  • >> We recommend you could create a extra log file to log individual test result.

    Hi

    Thanks for your response. But how to get the test result. From where I can get the test result? I tried but I couldn't get it.

    And, I would not like to run each test separately. I am running all tests through vstest.console.exe. When I run, for each test, it tells whether it is passed or failed. That means some where the result for each test getting stored. I would like to know where and how it is stored? And how can I get that test result?

    I would like to achieve the following progress for each test run:

    No.OfTestsPassed/TotalNo.OfTests    and

    No.OfTestsFailed/TotalNo.OfTests 


    Thanks & Regards

    Anil

    Wednesday, July 24, 2019 10:31 AM
  • Hi R Anil,

    Thank you for reply.

    According to your description, you could get test result from "TestContext.CurrentTestOutcome", please refer more here: TestMethod API.

    And I try to write a simple sample, Hope it could help you.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    
    namespace UnitTestProject5
    {
        [TestClass]
        public class UnitTest1
        {
            private TestContext testContextInstance;
            public TestContext TestContext
            {
                get { return testContextInstance; }
                set { testContextInstance = value; }
            }
    
            string numPass = File.ReadAllText(".\\CountPASS.txt");
            string numFail = File.ReadAllText(".\\CountFailed.txt");
    
            int actualTotal = 2;
    
            [TestMethod]
            public void TestMethod1()
            {
                //to do
            }
    
            [TestMethod]
            public void TestMethod2()
            {
                //to do
            }
    
            [TestCleanup]
            public void WriteToLogFile()
            {
                var testOutcome = TestContext.CurrentTestOutcome;
                string testName = TestContext.TestName;
    
                StringBuilder sb = new StringBuilder();
                string str = testName + ": " + testOutcome.ToString() + "\n" ;
                sb.Append(str);
    
    
                int numberPass = Convert.ToInt32(numPass);
                int numberFail = Convert.ToInt32(numFail);
    
                if (testOutcome.ToString() == "Passed")
                {
                    numberPass ++;
                    File.WriteAllText(@".\\CountPASS.txt", numberPass.ToString());
                }
    
                if(testOutcome.ToString() == "Failed")
                {
                    numberFail ++;
                    File.WriteAllText(@".\\CountFailed.txt", numberFail.ToString());
                }
    
                int totalnumber = numberPass + numberFail;
    
                string result = numberPass.ToString() + "/" + totalnumber.ToString() + "\n" + numFail.ToString() + "/" + totalnumber.ToString();
    
                if (totalnumber == actualTotal)
                {
                    sb.Append(result);
    
                    File.WriteAllText(@".\\CountPASS.txt", "0");
                    File.WriteAllText(@".\\CountFailed.txt", "0");
                }                
    
                File.AppendAllText("D:\\" + "log.txt", sb.ToString());
                sb.Clear();
            }
        }
    }

    Any feedback will be expected.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Thursday, July 25, 2019 9:17 AM
  • Hi Dylan,

    Thank you for the proposal. This also works for me. But I have done with some other way like below:

    I have defined my own custom logger by implementing ITestLogger and writing the TestOutcome of each test to a file at the end of each test run using the event TestResult provided by ITestLogger. So that I can get each test result and able to show the progress.

    And, as I am using vstest.console.exe to run tests, I am able to use multiple loggers. With this I am getting each test result with my custom logger and getting .trx report at the end of complete test run with TRX logger.

    Your response made me to think in a different direction to get the outcome. It really helped me. Thanks a lot.

    Thanks & Regards,

    Anil

    Friday, July 26, 2019 10:26 AM
  • Hi R Anil,

    Sorry for bothering you.

    I'm glad to hear that my reply could help you. And If possible, could please mark the useful reply as answer, that benefits for other community members who meet the similar issue, thank in advance.

    And if you have other issue about unit test, please feel free to contact us.We will try our best to provide a solution.

    Have a nice day.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    Tuesday, July 30, 2019 6:40 AM