c# - MSTEST - Test result file in MSTest shows all attachment for each of the test (Both Pass and Fail)

1.4k Views Asked by At

I have a selenium automation suite and the test cases has the capability to take a screenshot if a test fails.

All the test are unit tests and are datadriven. What I meant by data driven is that the test gets executed for each of the data row. please see sample code below

    [TestMethod]
    [TestCategory("UITest"), TestCategory("Flow")]
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\allFlows.xml", "flow", DataAccessMethod.Sequential)]
    public void MyTest()
    {
       //TEST CODE
    }

The above code is datadriven by the number of dataflows in allFlows.xml

The issue I am facing is when a test fails,a screenshot gets generated and I am able to see the screenshot attached to each of the test ( including the pass tests) I believe this is making the trx file heavy and ugly

Say for example I have 1 tests there is 5 flow data in allFlow.xml. So the total test executed is 5 and lets say that 2 tests failed.

The report says that 3 test passed and 2 test failed. I have now 2 screenshots for two failed tests.

The issue is the screenshot is getting attached to all the 3 passed test in addition to the 2 failed test.

Any idea how to resolve this issue? Do I need to live with it as it is a microsoft feature/ bug??

The issue is similar to this C# - .NET - MSTest - Test context - Add result file - Selenium Screenshots - Issue while viewing the result file

and I remember a similar issue was raised by someone and there was no resolution for this?

Is this a MSTest feature?

Any help would be great.

SCREENSHOT CODE IS BELOW

                Directory.CreateDirectory(testContext.TestResultsDirectory);
                Screenshot screenShot = ((ITakesScreenshot)driver).GetScreenshot();
                String CurrentIteration = testContext.DataRow.Table.Rows.IndexOf(testContext.DataRow).ToString();
                string fileName = testContext.TestResultsDirectory+ "\\Screenshot_" + testContext.TestName + "_RowNumber " + CurrentIteration + "_" + DateTime.Now.ToString("yyyy-dd-MM-HH-mm-ss") + ".png";
                screenShot.SaveAsFile((fileName), ImageFormat.Png);
                Wait.WaitBetweenSteps(3000);  

Thanks

*********************************EDIT**********************************

 [TestClass]
public class Driver
{
    IWebDriver driver;
    private TestContext testContext;
    public TestContext TestContext
    {
        get { return testContext; }

        set { testContext = value; }
    }


    [ClassInitialize]
    public static void ClassInit(TestContext testContext)
    {
        DriverData driverData = new DriverData();
        driverData.CreateTestData();
    }

    [TestInitialize]
    public void Init()
    {
    //Some Init Code
    }

    [TestMethod]
    [TestCategory("UITest"), TestCategory("Flow")]
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\allFlows.xml", "flow", DataAccessMethod.Sequential)]
    public void MyTest()
    {
       //TEST CODE
        Flow flow = new TestDataUtilities().generateFlowFromDataRow(testContext.DataRow);
       //Here if Failed then call GetScreenShot(testContext);
       try{
       //TEST CODE
       }
       catch(Exception){
       GetScreenShot(testContext);
       }
    }
}
            GetScreenShot(testContext)
            {
            Directory.CreateDirectory(testContext.TestResultsDirectory);
            Screenshot screenShot = ((ITakesScreenshot)driver).GetScreenshot();
            String CurrentIteration = testContext.DataRow.Table.Rows.IndexOf(testContext.DataRow).ToString();
            string fileName = testContext.TestResultsDirectory+ "\\Screenshot_" + testContext.TestName + "_RowNumber " + CurrentIteration + "_" + DateTime.Now.ToString("yyyy-dd-MM-HH-mm-ss") + ".png";
            screenShot.SaveAsFile((fileName), ImageFormat.Png);
            Wait.WaitBetweenSteps(3000);  
            }       
0

There are 0 best solutions below