Reputation:
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:
GetFiles(string testcase)
which I am unable to do so.Appreciate your help.
Upvotes: 0
Views: 349
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
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