gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 08:13:02 25 4
gpt4 key购买 nike

我在 selenium webdriver 中使用 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#。我想要的只是运行我的主项目文件,即 AutomationCode.cs ,并且测试用例的结果“通过/失败”将保存在 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。请注意我的详细描述,请帮助我在代码中将结果以 1 或 5 的形式传递到 testRail.pm 文件中。请指导我如何做到这一点以及需要进行哪些更改?

最佳答案

原则上,这与您问的问题相同herehere

这就是给出的答案...

For NUnit you can access the result and other details of the test using properties found in TestContext.CurrentContext.

For your problem you can add the following check to the test teardown method

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

For MSTest add the following property to your test class

public TestContext TestContext { get; set; } 

Then use it by adding the following to TestCleanup

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

关于c# - 如何将运行时的测试用例结果调用到另一个文件以更新 testRail 中的测试用例结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60844451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com