user5687551
user5687551

Reputation:

C#: What is the reason of not detecting if testcase failed

I am trying to detect if my testcase failed that it should do some process which I have mentioned it in my code below. I am successfully able to detect when my testcase passed.

I have added my code. My code here does is to navigate to google homepage if it passed then I should get a txt file with a "[MethodName] - Passed.txt" with a text in it Passed. Same for fail.

[Test]
public void Initialize()
{
    PropertiesCollection.driver = new TWebDriver();
    LoginPageObject objLogin = new LoginPageObject();
    string pathfile = @"file location";
    string sheetName = "Login";
    var excelFile = new ExcelQueryFactory(pathfile);
    var abc = from a in excelFile.Worksheet(sheetName).AsEnumerable()
              where a["ID"] == "3"
              select a;
    foreach (var a in abc)
    {
        PropertiesCollection.driver.Navigate().GoToUrl(a["URL"]);
    }
    foreach (var a in abc)
    {
        objLogin.Login(a["uname"], a["paswd"]);
    }
    StackFrame stackFrame = new StackFrame();
    MethodBase methodBase = stackFrame.GetMethod();
    string Name = methodBase.Name;
    GetFiles(Name);
}
 public void GetFiles(string testcase)
{

    if ((TestContext.CurrentContext.Result.Status == TestStatus.Failed) || (TestContext.CurrentContext.Result.State == TestState.Failure) || (TestContext.CurrentContext.Result.State == TestState.Ignored) || (TestContext.CurrentContext.Result.State == TestState.Error))
    {

        string destpath = (@"destination location");
        File.WriteAllText(Path.Combine(destpath, testcase + " - Failed" + ".txt"), "Failed");
    }
    else
    {
        string destpath = (@"destination location");
        File.WriteAllText(Path.Combine(destpath, testcase + " - Passed" + ".txt"), "Passed");
    }
}

Here, only else condition is identified but not with if condition.

Question: Can anyone identify what part I am missing for fail test case.

Note:

  1. Please forgive me if I have not explained properly. If you donot understand please ask question.
  2. For each part is actually an error. If there is an error it should create a txt file as per my code in GetFiles(string testcase) which I am unable to do so.
  3. I am using Nunit to identify whether my test case passed or fail. So if testcase passes or fails a text is created mentioning that to keep a record of my failed test case.

Appreciate your help.

Upvotes: 0

Views: 349

Answers (2)

Chris
Chris

Reputation: 6052

If a test fails, the [Test] method is terminated, and the rest of the method never run. So, if your test were to fail mid-way through, GetFiles() would never be called.

You probably mean to run GetFiles() as a [TearDown] to achieve what you're trying to do.

As an aside, I'd recommend doing:

TestContext.CurrentContext.Result.Status != TestStatus.Success

rather than you current if - as you seem to have found already, there are many different varieties of failure!

Upvotes: 1

Steve Wong
Steve Wong

Reputation: 2256

It looks like you have a typo. Your if statement can never be true as written. You want an 'or' instead of an 'and'

if ((TestContext.CurrentContext.Result.Status == TestStatus.Failed) ||
    (TestContext.CurrentContext.Result.State == TestState.Failure) ||
    (TestContext.CurrentContext.Result.State == TestState.Ignored) || // <-- typo?
    (TestContext.CurrentContext.Result.State == TestState.Error))
    {

Upvotes: 0

Related Questions