如何使用testng.xml运行测试文件

huangapple go评论75阅读模式
英文:

How to run the test files by using the testng.xml

问题

[![**项目结构**][1]][1]

[1]: https://i.stack.imgur.com/kuDGI.png

我按照上面的包结构创建了一个项目,我在运行通过我手动创建的 **testng.xml** 文件来运行测试时遇到了一个问题。

所有的 **测试类** 都位于 **API** 包下的各自包中,而 **API** 包包含了应用程序的每个模块作为包,测试类定义在它们各自的模块包中。

在附加的截图中,我仅引用要从 **testng.xml** 文件中运行的一个包,但当运行文件时,它显示为 "未找到测试"。

以下是 **ApprovalGroupManagementApi.class** 的代码段,其中包含有适当的 **TestNG** 注解的测试方法。

```java
package com.api.approval_group_management;
import com.base.Base;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.javafaker.Faker;
import com.payloads.request.classes.CreatedUserList;
import com.payloads.request.classes.StatusList;
import com.payloads.request.payload.SecCreateApprovalLevel;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;
import java.util.ArrayList;
import java.util.List;
import static com.utils.Constant.SEC_CREATE_APPROVAL_LEVEL_URL;
import static io.restassured.RestAssured.given;

public class ApprovalGroupManagementApi extends Base {

    SoftAssert softAssert = new SoftAssert();
    Assertion hardAssert = new Assertion();

    @Test(priority = 1, alwaysRun = true, enabled = true)
    public void secCreateApprovalLevel() {
        try {

            ObjectMapper objectMapper = new ObjectMapper();
            SecCreateApprovalLevel secCreateApprovalLevel = new SecCreateApprovalLevel();
            StatusList statusList = new StatusList();
            CreatedUserList createdUserList = new CreatedUserList();
            List<Object> objects = new ArrayList<>();

            secCreateApprovalLevel.setName("" + new Faker().name().firstName());
            secCreateApprovalLevel.setApprovalLevelAvailable(true);
            secCreateApprovalLevel.setStatusList(statusList);
            secCreateApprovalLevel.setCreatedUserList(createdUserList);
            secCreateApprovalLevel.setPrivilageList(objects);

            String approvalGroup = objectMapper.writeValueAsString(secCreateApprovalLevel);

            RestAssured.useRelaxedHTTPSValidation();
            Response response = given().spec(resourceApiRequestSpecification).log().all()
                    .contentType(ContentType.JSON)
                    .when()
                    .body(approvalGroup)
                    .post(SEC_CREATE_APPROVAL_LEVEL_URL);

            // 响应状态码断言
            hardAssert.assertEquals(response.getStatusCode(), 200);
            softAssert.assertAll();

        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

<details>
<summary>英文:</summary>

[![**Project Structure**][1]][1]


  [1]: https://i.stack.imgur.com/kuDGI.png


I have created a project as per the above package structure and I&#39;m facing an issue on running the test&#39;s
by the **testng.xml** file I manually created.

All the **test classes** reside in their own packages under the **API** package, and the **API** package consists of each module of the application as packages where test classes are defined in their respective module packages.

In the attached screenshot im referring only one package to be run from the **testng.xml** file and when running the file it displays as &quot;No tests were found&quot;

below is a code segment of **ApprovalGroupManagementApi.class** where the test methods are in with propper **TestNG** annotations.

package com.api.approval_group_management;
import com.base.Base;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.javafaker.Faker;
import com.payloads.request.classes.CreatedUserList;
import com.payloads.request.classes.StatusList;
import com.payloads.request.payload.SecCreateApprovalLevel;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;
import java.util.ArrayList;
import java.util.List;
import static com.utils.Constant.SEC_CREATE_APPROVAL_LEVEL_URL;
import static io.restassured.RestAssured.given;

public class ApprovalGroupManagementApi extends Base {

SoftAssert softAssert = new SoftAssert();
Assertion hardAssert = new Assertion();

@Test(priority = 1, alwaysRun = true, enabled = true)
public void secCreateApprovalLevel() {
    try {

        ObjectMapper objectMapper = new ObjectMapper();
        SecCreateApprovalLevel secCreateApprovalLevel = new SecCreateApprovalLevel();
        StatusList statusList = new StatusList();
        CreatedUserList createdUserList = new CreatedUserList();
        List&lt;Object&gt; objects = new ArrayList&lt;&gt;();

        secCreateApprovalLevel.setName(&quot;&quot; + new Faker().name().firstName());
        secCreateApprovalLevel.setApprovalLevelAvailable(true);
        secCreateApprovalLevel.setStatusList(statusList);
        secCreateApprovalLevel.setCreatedUserList(createdUserList);
        secCreateApprovalLevel.setPrivilageList(objects);

        String approvalGroup = objectMapper.writeValueAsString(secCreateApprovalLevel);

        RestAssured.useRelaxedHTTPSValidation();
        Response response = given().spec(resourceApiRequestSpecification).log().all()
                .contentType(ContentType.JSON)
                .when()
                .body(approvalGroup)
                .post(SEC_CREATE_APPROVAL_LEVEL_URL);

        //response code assertion
        hardAssert.assertEquals(response.getStatusCode(), 200);
        softAssert.assertAll();

    } catch (Exception e) {
        System.out.println(e);
    }
}

}

答案1

得分: 1

将软件包名称更改为 name="com.api.approval_group_management" 在 testng.xml 文件中。

英文:

Change the package name to name=&quot;com.api.approval_group_management&quot; in testng.xml

答案2

得分: 0

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="suite 1">
    <test name="Regression 1">
        <classes>
            <class name="test.java.com.api.approval_group_management.ApprovalGroupManagementApi" />
        </classes>
    </test>
</suite>
英文:
&lt;!DOCTYPE suite SYSTEM &quot;http://testng.org/testng-1.0.dtd&quot;&gt;
&lt;suite name=&quot;suite 1&quot;&gt;
	&lt;test name=&quot;Regression 1&quot;&gt;
		&lt;classes&gt;
			&lt;class name=&quot;test.java.com.api.approval_group_management.ApprovalGroupManagementApi&quot; /&gt;
		&lt;/classes&gt;
	&lt;/test&gt;
&lt;/suite&gt;

Please try this It might help you to execute testng class.
(Apart from this also make sure the plugin of testng is already installed.)

huangapple
  • 本文由 发表于 2020年9月20日 18:01:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63977708.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定