如何在运行时将测试用例结果调用到另一个文件以更新testRail中的测试用例结果



我在selenium网络驱动程序中使用MSTEST C#。我的项目层次是

Level1-MainProjectfile
Level2-Properties
Level2-Refernces
Level2-AppObj(folder)
Level3-DP(folder)
Level4-dpo.cs
Level4-dpc.cs
Level3-TestRail(folder)
Level4-TestRailpm.cs
Level4-TestRailpo.cs
Level3-gmethods.cs
Level2-AUtomationCode.cs
Level2-log4net.config

现在我的单元测试用例出现在AutomationCode.cs文件中,这是主要的项目文件。我的AutomationCode.cs文件中的代码是

public class AutomationCode
{
private IWebDriver WDriver;
private log4net.ILog Testlog;


[TestInitialize]
public void wd()
{
WDriver = Helper.GetWebDriver(helperconst.browserType.Chrome);
Testlog = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
}
[Priority(1)]
[TestMethod]
public void search()
{
Testlog.Info("Test ID:001, This test will select the criteria and update the customer dropdown");

Testlog.Info("Step 1/1 : Select customer from cutomer dropdown");
var dc = gmethods.GetSelectElement(WDriver, dpo.customermenu);
dc.SelectByText(dpc.customer);
}
///[Ignore]
[Priority(2)]
[Microsoft.VisualStudio.TestTools.UnitTesting.TestMethod]
public void TestRailCall()
{
TestRailpm a = new TestRailPM();
a.testrail();
}
[TestCleanup]
public void CleanUp()
{
WDriver.Close();
WDriver.Quit();
}
}

在dpo页面中:

public static class dpo
{
public const string customermenu = "[data]";
}

在dpc页面

public static class dpc
{
public const string customer = "A";
}

在gmethods页面中:

static public SelectElement GetSelectElement(IWebDriver drv, string elem)
{
SelectElement a = new SelectElement(drv.FindElement(By.CssSelector(elem)));
return a;
}

在TestRailPO文件中,代码为

namespace Automation.AppObj.TestRail
{
public class TestRailPO
{
public class TestResultKeeper
{
public static TestResult TestResult { get; set; }
public static UnitTestOutcome TestOutcome => UnitTestOutcome.Failed;
//TestResult ?? Outcome ?? UnitTestOutcome.Failed;
public static bool IsPassed => TestOutcome == UnitTestOutcome.Passed;
public static Exception Exception { get; set; }

}
public class TestMethodAttribute : Attribute
{
public virtual TestResult[] Execute(ITestMethod testMethod)
{
return new TestResult[] { };
}
}
public class LogTestTestMethod : TestMethodAttribute
{
public override TestResult[] Execute(ITestMethod testMethod)
{
var testResult = base.Execute(testMethod)[0];
TestResultKeeper.TestResult = testResult;
//TestResultKeeper.Exception = testResult.TestFailureException;
return new[] { testResult };

}
}
}

在testrailpm.cs中,代码为:

public class TestRailPM
{

string testRailUrl = "https://test.testrail.io/";
string testRailUser = "test@gmailcom";
string testRailPassowrd = "test";
int projectId = 1;
int milestoneId = 1;
int suiteId = 1;
int testRailUserId = 1;
string[] testCaseIds = { "12345", "21343" };

public void testrail()
{
//Create Test Run in TestRail
//Pass "true" against 'includeAll' parameter to insert all test cases in a suite; "false" to add specific test case id
string testRunID = CreateTestRun.CreateRun(testRailUrl, testRailUser, testRailPassowrd, projectId, suiteId, milestoneId, "Automation of TestCases", "Automation of TestCases", testRailUserId, false, testCaseIds);
int testRunIdInInt = Convert.ToInt16(testRunID);
//Get TestCases Ids of a Run
int[] newTestRunCaseIds = GetTestCases.getTestCaseIds(testRailUrl, testRailUser, testRailPassowrd, testRunIdInInt, true);
int[] originalTestCaseIds = GetTestCases.getTestCaseIds(testRailUrl, testRailUser, testRailPassowrd, testRunIdInInt, false);
//Add Result for Single Test Case in a Test Run
/* testCaseStatus int The ID of the test status. The built-in test rail statuses have the following IDs:
1 Passed
2 Blocked
3 Untested (not allowed when adding a result)
4 Retest
5 Failed
*/

int singleTestCaseId = 716869;
UpdateTestRun.updateSingleTestCaseInATestRun(testRailUrl, testRailUser, testRailPassowrd, testRunIdInInt, singleTestCaseId, TestContext.CurrentTestOutcome == UnitTestOutcome.Passed ? 1 : 5, "testCaseComments", testRailUserId);

/*
// Add Result for Multiple Test Cases in a Test Run at a time
int[] testCaseIdsToUpdateResults = { 584003, 584004, 584005, 584006, 584007, 584008, 584009, 584075, 584076, 584213, 604458, 716869, 716870, 716871, 716872};
int[] testCaseStatuses = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
string[] testCaseComments = { "This case is passed", "This case is passed", "This case is passed", "This case is passed", "This case is passed", "This case is passed", "This case is passed", "This case is passed", "This case is passed", "This case is passed", "This case is passed", "This case is passed", "This case is passed", "This case is passed", "This case is passed"};
UpdateTestRun.UpdateTestRunResults(testRailUrl, testRailUser, testRailPassowrd, testRunIdInInt, newTestRunCaseIds, testCaseStatuses, testCaseComments, testRailUserId);
*/

}

}

我正在使用MStest C#。我只想运行我的主项目文件CCD_ 1和测试用例的结果;通过/失败";将保存在TestRailpo.cs文件testkeeperresult或任何其他属性中的变量或属性等中。当然,保存的结果要么是通过,要么是失败,但最重要的是。我需要用1或5的数字形式传递这个结果。1表示通过,5表示不通过。我需要在Resultoftestcase中的TestRailpm.cs文件中传递该结果。

UpdateTestRun.updateSingleTestCaseInATestRun(testRailUrl, testRailUser, testRailPassowrd, testRunIdInInt, singleTestCaseId, Resultoftestcase, "testCaseComments", testRailUserId);
}

在testcleanup之前,我已经将TestRail.pm文件中的TestRail((方法调用到AutomationCode.cs,因为我想在执行单元测试用例后更新TestRail。考虑到我的详细描述,请在代码中帮助我在testRail.pm文件中以1或5的形式传递结果。请指导我如何做到这一点,需要做哪些更改?

原则上,这与您在这里和这里问的问题相同

这是给出的答案。。。

对于NUnit,您可以访问测试的结果和其他详细信息使用TestContext.CurrentContext.中的属性

对于您的问题,您可以在测试拆卸中添加以下检查方法

if(TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Passed) { .... }

对于MSTest,将以下属性添加到测试类中

public TestContext TestContext { get; set; } 

然后将以下内容添加到TestCleanup中使用

if(TestContext.CurrentTestOutcome == UnitTestOutcome.Passed) { .... }

请在自动化代码类中添加一个测试上下文属性,如下所示:

public class AutomationCode
{
private IWebDriver WDriver;
private log4net.ILog Testlog;
public TestContext TestContext { get; set; }
}

现在在cleanup((方法中添加以下代码,调用将更新TestRail中测试结果的方法。类似这样的东西:

[TestCleanup]
public void CleanUp()
{
WDriver.Close();
WDriver.Quit();
TestRailpm a = new TestRailPM();
if (TestContext.CurrentTestOutcome.Equals(UnitTestOutcome.Passed))
{
UTestResultKeeper.TestResult.Outcome = UnitTestOutcome.Passed;
a.Resultoftestcase = "1";
}
else if (TestContext.CurrentTestOutcome.Equals(UnitTestOutcome.Failed)|| TestContext.CurrentTestOutcome.Equals(UnitTestOutcome.Timeout)|| TestContext.CurrentTestOutcome.Equals(UnitTestOutcome.Inconclusive))
{
TestResultKeeper.TestResult.Outcome = UnitTestOutcome.Failed;
a.Resultoftestcase = "5";
}
//now that TestReultKeeper is updated wi can call the API method to update the test rail
a.testrail();
}

我不确定是否可以访问TestRailpm.Resultoftestcase属性,因为您没有共享整个代码,但我直接调用它来更新属性,稍后在Testrail中更新结果。

请告诉我,如果这对您不起作用,我有一种更简单的方法来使用相同的testrail API更新testrail中的测试结果。

实现这一点的更简单的方法是从这里下载TestRail API客户端,然后使用TestRail API 更新TestRail测试用例

public class UpdateTestResult
{
/// <summary>
/// Update the test result in testrail
/// </summary>
/// <param name="serverUrl">e.g. "http://<server>/testrail/</param>
/// <param name="userName">username to be used</param>
/// <param name="userPassword">password of the user</param>
/// <param name="testCaseID">testrail test case ID</param>
/// <param name="testResult">stauts of the test. e.g. :1 is passed and 5 is failed.</param>
public void UpdateResultInTestRail(string serverUrl, string userName, string userPassword, string testCaseID, int testResult)
{
APIClient client = new APIClient("http://<server>/testrail/") { User = "userName", Password = "userPassword" };
var data = new Dictionary<string, object>
{
{ "status_id", testResult },
{ "comment", "This test worked fine!" }
};
client.SendPost("add_result/:"+ testCaseID, data);
}
}

现在您可以在测试方法的cleanup方法中调用这个类方法。类似这样的东西:

[TestCleanup]
public void CleanUp()
{
UpdateTestResult updateResult = new UpdateTestResult();
if (TestContext.CurrentTestOutcome.Equals(UnitTestOutcome.Passed))
{
updateResult.UpdateResultInTestRail("serverUrl", "username", "Password", "testcaseID", 1);
}
else if (TestContext.CurrentTestOutcome.Equals(UnitTestOutcome.Failed)|| TestContext.CurrentTestOutcome.Equals(UnitTestOutcome.Timeout)|| TestContext.CurrentTestOutcome.Equals(UnitTestOutcome.Inconclusive))
{
updateResult.UpdateResultInTestRail("serverUrl", "username", "Password", "testcaseID", 5);
}
}

要了解更多关于testrail API的信息,您可以参考此链接。对于API结果,您甚至可以参考此链接。

相关内容

  • 没有找到相关文章

最新更新