Junit 动态测试用例从文件读取数据?

huangapple 未分类评论50阅读模式
英文:

Junit Dynamic Test Cases data reading from file?

问题

我有一个场景,需要使用来自文本文件的载荷来测试 API。文件中的每一行代表一个载荷。如何根据上述场景动态生成测试用例。

我尝试了以下方法,从一个测试调用另一个测试,但我只能看到父测试通过。

import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.response.Response;

import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.jayway.restassured.RestAssured.given;

public class ExampleTest
{

    private TestUtil testUtil;

    String payload;

    @Before
    public void init()
    {
        testUtil = new TestUtil();
    }

    @Test
    public void runAllTests() throws IOException
    {
        List<String> request = getFileDataLineByLine();

        for(String fileRequest:request)
        {
            payload=fileRequest;
            if(null!=payload) {
                testExampleTest();
            }
        }
    }

    @Test
    public void testExampleTest()
    {
        String uri = "http://localhost:8080/url";
        Response response = given()
                .contentType(ContentType.JSON)
                .body(payload)
                .post(uri)
                .then()
                .statusCode(200)
                .extract()
                .response();
    }

    private List<String>  getFileDataLineByLine() throws IOException
    {
        File file = testUtil.getFileFromResources();
        if (file == null)
            return null;
        String line;
        List<String> stringList = new ArrayList<>();
        try (FileReader reader = new FileReader(file);
             BufferedReader br = new BufferedReader(reader))
        {
            while ((line = br.readLine()) != null)
            {
                stringList.add(line);
                System.out.println(line);
            }
        }
        return stringList;
    }
}

文件读取类:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;

public class TestUtil
{

    public File getFileFromResources()throws IOException
    {
        TestUtil testUtil = new TestUtil();
        File file = testUtil.getFileFromResources("testdata.txt");
        return file;
    }

    // get file from classpath, resources folder
    private File getFileFromResources(String fileName)
    {
        ClassLoader classLoader = getClass().getClassLoader();
        URL resource = classLoader.getResource(fileName);
        if (resource == null) {
            throw new IllegalArgumentException("file is not found!");
        } else {
            return new File(resource.getFile());
        }
    }
}

如何通过从文件中获取输入来动态生成测试用例呢?

英文:

I have a scenario where to test the api using the payload coming from text file.Each line in file represents one payload.How can I dynamically generate test cases based on the above scenario.

I tried as below calling one test from other ,but i can only see paraent test passed.

import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.response.Response;

import org.junit.Before;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.jayway.restassured.RestAssured.given;

public class ExampleTest
{


    private TestUtil testUtil;


    String payload;


    @Before
    public void init()
    {
            testUtil = new TestUtil();
    }

    @Test
    public void runAllTests() throws IOException
    {


        List&lt;String&gt; request = getFileDataLineByLine();

        for(String fileRequest:request)
        {
            payload=fileRequest;
            if(null!=payload) {
                testExampleTest();
            }
        }
    }

    @Test
    public void testExampleTest()
    {

        String uri = &quot;http://localhost:8080/url&quot;;
        Response response = given()
                .contentType(ContentType.JSON)
                .body(payload)
                .post(uri)
                .then()
                .statusCode(200)
                .extract()
                .response();

    }


    private List&lt;String&gt;  getFileDataLineByLine() throws IOException {

        File file = testUtil.getFileFromResources();
        if (file == null)
            return null;
        String line;
        List&lt;String&gt; stringList = new ArrayList&lt;&gt;();
        try (FileReader reader = new FileReader(file);
             BufferedReader br = new BufferedReader(reader))
        {
            while ((line = br.readLine()) != null)
            {
                stringList.add(line);
                System.out.println(line);
            }
        }

        return stringList;
    }

File Reading Class:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;

public class TestUtil
{

    public File getFileFromResources()throws IOException
    {

        TestUtil testUtil = new TestUtil();
        File file = testUtil.getFileFromResources(&quot;testdata.txt&quot;);
        return file;
    }


    // get file from classpath, resources folder
    private File getFileFromResources(String fileName) {

        ClassLoader classLoader = getClass().getClassLoader();

        URL resource = classLoader.getResource(fileName);
        if (resource == null) {
            throw new IllegalArgumentException(&quot;file is not found!&quot;);
        } else {
            return new File(resource.getFile());
        }

    }



}

How can i generate test cases dynamically by taking input from file?

答案1

得分: 0

如果您可以将您的文件转换,JUnitParams支持从CSV加载数据。

使用上面链接中存储库的示例:

public class PersonTest {

    @Test
    @FileParameters("src/test/resources/test.csv")
    public void loadParamsFromCsv(int age, String name) {
        assertFalse(age > 120);
    } 
}
英文:

If you can convert your file JUnitParams supports loading data from a CSV.

Using an example from the above link's repository:

public class PersonTest {

    @Test
    @FileParameters(&quot;src/test/resources/test.csv&quot;)
    public void loadParamsFromCsv(int age, String name) {
        assertFalse(age &gt; 120);
    } 
}

</details>



huangapple
  • 本文由 发表于 2020年5月4日 00:20:20
  • 转载请务必保留本文链接:https://java.coder-hub.com/61577808.html
匿名

发表评论

匿名网友

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

确定