我试图获得一个测试用例的类别值。测试用例的类别值为
[TestFixture]
[Parallelizable(ParallelScope.Self)]
[Category("Smoke")]
public class MSPSmokeTests : Base
{
[Test, TestCaseSource(nameof(LoginDataZZ3))]
[Category("C1879186")]
在我的teardown方法中,我尝试访问这个类别值并删除'C'值以将其存储到var变量中,只留下数字'1979186。
var testCase = TestContext.CurrentContext.Test.Properties.Get("Category").ToString().Replace("C", "");
我得到以下错误时,我运行:
系统。NullReferenceException: 'Object reference not set to an Object instance .'
NUnit.Framework.TestContext.PropertyBagAdapter.Get(…)返回null.
这里是否有错误的逻辑或其他方法来做到这一点?
相同的代码在解决方案的另一部分工作,如下所示:
[Test, TestCaseSource(nameof(LoginDataZZ3))]
[Category("C1879186")]
public void UserCanClickOnAdministrationTileTest(string username, string password)
{
LoginPage loginPage = new LoginPage(GetDriver());
MSPDashboard mspdashboard = new MSPDashboard(GetDriver());
driver.Value.Navigate().GoToUrl(zz3);
loginPage.CheckCookies();
loginPage.EnterUserNameAndPassword(username, password);
driver.Value.Navigate().GoToUrl($"{zz3}/msp/index");
mspdashboard.adminTile.Click();
string currentURL = driver.Value.Url;
StringAssert.Contains("/msp/orgs", currentURL);
}
拆卸方法:
[TearDown]
public async Task TearDown()
{
// arrange
RestRequest restRequest = new RestRequest($"index.php?/api/v2/get_runs/7&limit=1", Method.Get);
string authInfo = Base64StringConverter.GetBase64String("email:password");
restRequest.AddHeader("Authorization", "Basic " + authInfo);
restRequest.AddHeader("Content-Type", "application/json");
// act
RestResponse<GetRun> response = await restClient.ExecuteAsync<GetRun>(restRequest);
HttpStatusCode statusCode = response.StatusCode;
// assert
var data = JsonConvert.DeserializeObject<List<GetRun>>(response.Content);
var a = data[0];
var run = a.Id;
var testCase = TestContext.CurrentContext.Test.Properties.Get("Category").ToString().Replace("C", "");
Console.WriteLine(testCase);
var result = TestContext.CurrentContext.Result.Outcome.Status;
var testRailStatus = result switch
{
TestStatus.Failed => ResultStatus.Failed,
TestStatus.Passed => ResultStatus.Passed,
_ => ResultStatus.Retest
};
Console.WriteLine(testRailStatus.ToString());
string status = testRailStatus.ToString();
int status_id;
if (status == "Passed")
{
status_id = 1;
}
else status_id = 5;
Console.WriteLine(status_id);
RestRequest addResult = new RestRequest($"index.php?/api/v2/add_result_for_case/{run}/{testCase}", Method.Post);
addResult.AddHeader("Authorization", "Basic " + authInfo);
addResult.AddHeader("Content-Type", "application/json");
addResult.RequestFormat = DataFormat.Json;
addResult.AddJsonBody(new { status_id = status_id });
var newResponse = await restClient.ExecuteAsync(addResult);
}
您没有获得类别值,因为它没有正确设置。使用TestCaseSource时设置类别的正确方法如下:
[TestFixture]
[Parallelizable(ParallelScope.Self)]
[Category("Smoke")]
public class MSPSmokeTests : Base
{
[Test, TestCaseSource(nameof(LoginDataZZ3), Category = "C1234")]
public void TestMethod1(string username, string password)
{
}
}
还要注意,[TearDown]是在每个测试方法之后执行的,所以要确保为每个TestCases设置了Category,否则它会在没有设置的情况下失败。
您还可以向测试用例添加多个类别,如下所示:
[Test, TestCaseSource(nameof(LoginDataZZ3), Category = "C1234, FrontEndTests")]
然后可以读作:
var categories = TestContext.CurrentContext.Test.Properties["Category"].ToList();
Console.WriteLine(categories[0]);
Console.WriteLine(categories[1]);