user1208480
user1208480

Reputation:

C# Unable to get data in if condition with Test Attribute

I am trying to add some texts in txt file format by [Teardown] attribute, it checks whether testcase pass or fail and accordingly it adds data into text file. Pass or fail is detected by if-else condition. Else condition (Passed Test) work fine. But when I am tying to add with Failed test it does not gives result which is expected

For Passed Test - Result

Test Case No.: 12345
Test Details: For test purpose
Start Time: 3/2/2016 3:51:28 PM
End Time: 3/2/2016 3:51:31 PM
Total Time: 0 minutes and 2 seconds
Status: Passed

For Failed Test - Result

Test Case No.: 
Test Details: 
Start Time: 3/2/2016 3:49:37 PM
End Time: 1/1/0001 12:00:00 AM
Total Time: -49 minutes and -37 seconds
Status: Failed

From the Failed test result, I am not getting below things

  1. Test Case No.
  2. Test Details:
  3. End Time:
  4. Total Time:

Below is my code:

[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
[TestFixture(typeof(ChromeDriver))]
public class TestWithMultipleBrowsers<TWebDriver> where TWebDriver : IWebDriver, new()
{
    static DateTime _timestamp1;
    static DateTime _timestamp2;
    static string _TestNum;
    static string _TestDetails;
[Test]
public void SurfGoogle()
{
    _timestamp1 = DateTime.Now;
    TWebDriver driver = new TWebDriver();
   driver.Navigate().GoToUrl("http://www.google.com");
  //driver.FindElement(By.Id("q"));
   _timestamp2 = DateTime.Now;
   _TestNum = "12345";
   _TestDetails = "For test purpose";
}


[TearDown]
public void teardown()
{

    string path = (@"..\..\Result\");
    TimeSpan timediff = _timestamp2 - _timestamp1;
    string TotalTime = timediff.Minutes + " minutes and " + timediff.Seconds + " seconds";

    if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)
    {
        string status = "Failed";

        File.WriteAllText(Path.Combine(path, "Failed" + ".txt"), "Test Case No.: " + _TestNum + string.Format(Environment.NewLine) + "Test Details: " + _TestDetails + string.Format(Environment.NewLine) + "Start Time: " + _timestamp1 + string.Format(Environment.NewLine) + "End Time: " + _timestamp2 + string.Format(Environment.NewLine) + "Total Time: " + TotalTime + string.Format(Environment.NewLine) + "Status: " + status);
    }
    else
    {
        string status = "Passed";

        File.WriteAllText(Path.Combine(path, "Passed" + ".txt"), "Test Case No.: " + _TestNum + string.Format(Environment.NewLine) + "Test Details: " + _TestDetails + string.Format(Environment.NewLine) + "Start Time: " + _timestamp1 + string.Format(Environment.NewLine) + "End Time: " + _timestamp2 + string.Format(Environment.NewLine) + "Total Time: " + TotalTime + string.Format(Environment.NewLine) + "Status: " + status);
    }   
  }
}

To Fail the test you can remove commented part from my code.

Using NUnit: 2.6.4

Upvotes: 2

Views: 196

Answers (2)

user1413
user1413

Reputation: 537

[Test]
public void SurfGoogle()
{
    _timestamp1 = DateTime.Now;  
    _TestNum = "12345";                  <-------
    _TestDetails = "For test purpose";   <-------

    TWebDriver driver = new TWebDriver();
    driver.Navigate().GoToUrl("http://www.google.com");
    driver.FindElement(By.Id("q"));
  }
}

[TearDown]
public void teardown()
{

    string path = (@"..\..\Result\");
    _timestamp2 = DateTime.Now;          <-------
    TimeSpan timediff = _timestamp2 - _timestamp1;
    string TotalTime = timediff.Minutes + " minutes and " + timediff.Seconds + " seconds";

    if (TestContext.CurrentContext.Result.Status == TestStatus.Failed)
    {
        string status = "Failed";

        File.WriteAllText(Path.Combine(path, "Failed" + ".txt"), "Test Case No.: " + _TestNum + string.Format(Environment.NewLine) + "Test Details: " + _TestDetails + string.Format(Environment.NewLine) + "Start Time: " + _timestamp1 + string.Format(Environment.NewLine) + "End Time: " + _timestamp2 + string.Format(Environment.NewLine) + "Total Time: " + TotalTime + string.Format(Environment.NewLine) + "Status: " + status);
    }
    else
    {
        string status = "Passed";

        File.WriteAllText(Path.Combine(path, "Passed" + ".txt"), "Test Case No.: " + _TestNum + string.Format(Environment.NewLine) + "Test Details: " + _TestDetails + string.Format(Environment.NewLine) + "Start Time: " + _timestamp1 + string.Format(Environment.NewLine) + "End Time: " + _timestamp2 + string.Format(Environment.NewLine) + "Total Time: " + TotalTime + string.Format(Environment.NewLine) + "Status: " + status);
    }   
  }

Try doing this, this might help you. Charlie is right but I think if you do this will solve your problem.

When NUnit catches an error it comes out of the method from the point where it broke. So the way I did will help to get this data as well. There are three changes and I have marked with this <-------

Upvotes: 0

Charlie
Charlie

Reputation: 13736

If you remove commenting and the call throws an exception, the following lines are never executed so the values are not set.

Upvotes: 0

Related Questions