Java 用 TestNG 丢失初始化变量

Java with TestNG lose initialized variable

出现了奇怪的情况,我无法理解。我有 RestAssured(宠物项目)的测试自动化。我想在 @BeforeSuite 方法中准备 RequestSpecification spec 变量,在 parent class 中。然后我想在 child class 的测试中使用 spec 变量。这是 parent class

的代码
public class BaseTest {

private String baseUrl = "https://api.thecatapi.com/v1/";
protected RequestSpecification spec;

@BeforeSuite
public void beforeSuite() {
    String apiKey = System.getProperty("api_key");
    spec = new RequestSpecBuilder()
            .setContentType(ContentType.JSON)
            .setBaseUri(baseUrl)
            .addHeader("x-api-key", apiKey)
            .addFilter(new ResponseLoggingFilter())
            .addFilter(new RequestLoggingFilter())
            .addFilter(new AllureRestAssured())
            .build();
    }
}

这是 child class'

中的一个测试
public class OurLittleTest extends BaseTest {

@Test
public void test() {
    //1
    String id = Breeds.search(spec, "Scottish Fold").then().extract().body().jsonPath().get("[0].id");

问题是测试中的 spec 变量为空,而我在等待时它不会为空...

为什么会这样?为什么 spec 为空?

我已经记录了一个 screencast 你可以看到我的问题

更新 Junit 5 一切正常。spec 变量在 child classes 中不再为空。这是我的 build.gradle 个文件:

使用 TestNG:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "io.qameta.allure:allure-gradle:2.8.1"
    }
}

plugins {
    id 'java'
    id 'io.qameta.allure' version '2.8.1'
}

sourceCompatibility = JavaVersion.VERSION_1_8

allure {
    configuration = 'testImplementation'
    version = '2.7.0'
    allureJavaVersion = '2.7.0'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.5'
    implementation 'org.projectlombok:lombok:1.18.20'
    annotationProcessor 'org.projectlombok:lombok:1.18.20'
    implementation 'io.rest-assured:rest-assured:4.4.0'
    implementation group: 'io.qameta.allure', name: 'allure-rest-assured', version: '2.14.0'
    implementation "io.qameta.allure:allure-testng:2.14.0"
    testImplementation group: 'org.testng', name: 'testng', version: '7.3.0'
}

test {
    useTestNG() {
        parallel = 'methods'
        threadCount = 2
    }
    if (project.hasProperty("api_key")) {
        systemProperties.put("api_key", "$api_key")
    }
}

使用 Junit 5:

plugins {
    id 'io.qameta.allure' version '2.5'
    id 'java'
}

allure {
    configuration = 'testImplementation'
    version = '2.7.0'

    useJUnit5 {
        version = '2.7.0'
    }

}

sourceCompatibility = JavaVersion.VERSION_1_8

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    jcenter()
    mavenCentral()
}

dependencies {
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.5'
    implementation 'org.projectlombok:lombok:1.18.20'
    annotationProcessor 'org.projectlombok:lombok:1.18.20'
    implementation 'io.rest-assured:rest-assured:4.4.0'
    implementation group: 'io.qameta.allure', name: 'allure-rest-assured', version: '2.14.0'
    implementation group: 'io.qameta.allure', name: 'allure-junit5', version: '2.14.0'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.2.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.2.0'
}

test {
    useJUnitPlatform{
        systemProperty 'junit.jupiter.testinstance.lifecycle.default'         , 'per_class'
        systemProperty 'junit.jupiter.execution.parallel.enabled',              'true'
        systemProperty 'junit.jupiter.execution.parallel.mode.default',         'same_thread'
        systemProperty 'junit.jupiter.execution.parallel.mode.classes.default', 'concurrent'
    }
    if (project.hasProperty("api_key")) {
        systemProperties.put("api_key", "$api_key")
    }
}

TestNG 有什么问题?

我测试了你的代码,没有错误。问题可能出在 OurLittleTest class 您使用 Junit,而您在 BaseTest class

使用 testNG

问题是 @BeforeSuite 的用法。它意味着每个套件只能使用一次,并在后续运行中被忽略。

要初始化规范,您可以像 protected RequestSpecification spec = new RequestSpecBuilder()... 一样直接在字段中内联,也可以使用 @BeforeClass.