简体   繁体   English

C#:未检测到测试用例失败的原因是什么

[英]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. 我在这里执行的代码是导航到Google主页(如果通过的话),那么我应该获得带有“ [MethodName]-Passed.txt”和Passed文本的txt文件。 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. 在此,仅识别其他条件,而不识别if条件。

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. 如果有错误,则应按照我在GetFiles(string testcase)代码创建一个txt文件,但我无法这样做。
  3. I am using Nunit to identify whether my test case passed or fail. 我正在使用Nunit来确定我的测试用例是否通过。 So if testcase passes or fails a text is created mentioning that to keep a record of my failed test case. 因此,如果测试用例通过或失败,则会创建一个文本,提及该文本以记录失败的测试用例。

Appreciate your help. 感谢您的帮助。

If a test fails, the [Test] method is terminated, and the rest of the method never run. 如果测试失败,则[Test]方法将终止,并且该方法的其余部分将永远不会运行。 So, if your test were to fail mid-way through, GetFiles() would never be called. 因此,如果您的测试在中途失败,则永远不会调用GetFiles()

You probably mean to run GetFiles() as a [TearDown] to achieve what you're trying to do. 您可能打算将GetFiles()作为[TearDown]以实现您要执行的操作。

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! 而不是您当前的情况-您似乎已经发现,失败的种类很多!

It looks like you have a typo. 看来您有错字。 Your if statement can never be true as written. 您的if陈述永远不会如写的那样真实。 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))
    {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM