当测试有4个testfixture时, OneTimeSetup方法被调用4次



我遇到了一个问题,我的并行UI Selenium测试多次调用[OneTimeSetup]方法,结果创建了多个testail测试运行。

在我的基类中,我的[OneTimeSetup]方法,我正在Testrail中创建一个teststron。我的测试包含4个[TestFixtures],每个fixture中的所有测试在每个fixture中调用一次[OneTimeSetup]方法。

我希望[OneTimeSetup]方法只被调用一次,即使我有4个(或更多)[TestFixtures]。

是否有一个NUnit属性,将只运行一次在多个[TestFixtures]?

代码:

public ThreadLocal<IWebDriver> driver = new ThreadLocal<IWebDriver>();
[OneTimeSetUp]
public async Task OneTimeSetUp()
{
RestRequest newRequest = new RestRequest($"index.php?/api/v2/add_run/{project_id}", Method.Post);
string authInfo = Base64StringConverter.GetBase64String("email:password");
newRequest.AddHeader("Authorization", "Basic " + authInfo);
newRequest.AddHeader("Content-Type", "application/json");
newRequest.RequestFormat = DataFormat.Json;
newRequest.AddJsonBody(new { assignedto_id = 1, suite_id = 1111, name = "UI Nightly Regression Test Run", refs = "UI Auto Tests", description = "This is a description of the test", include_all = true });
//act
var newResponse = await restClient.ExecuteAsync(newRequest);
HttpStatusCode statusCode = newResponse.StatusCode;
JObject responseData = JObject.Parse(newResponse.Content);
JToken jToken = responseData.SelectToken("id");
string test = jToken.ToString();
Console.WriteLine(test.ToString());
//assert
Console.WriteLine(newResponse.Content);
Console.WriteLine(newResponse.Request);
Console.WriteLine((int)statusCode);
Assert.That((int)statusCode, Is.EqualTo(200));
}

夹具测试示例:

[TestFixture]
[Parallelizable(ParallelScope.Self)]
[Category("Dashboard")]
public class DashboardTests : Base
{
[Test, TestCaseSource(nameof(LoginData), Category = "C1886640")]
[Category("Smoke")]

我在这里找到了答案:是否有可能对所有测试进行[OneTimeSetup] ?

将以下检查添加到我的基类中意味着它只在多个[TestFixtures]上运行一次。

private static bool FirstInitializationFlag = true;
[OneTimeSetUp]
public void PrepareTestsStart()
{
if (FirstInitializationFlag)
{
FirstInitializationFlag = false;

// Do actions before all test classes to run
}
}

相关内容

  • 没有找到相关文章

最新更新