为多个功能文件创建通用测试数据 [英] Creating common test data for multiple feature files

查看:53
本文介绍了为多个功能文件创建通用测试数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求如下:

  1. 我有几个.feature文件.我想创建所有这些功能文件都通用的测试数据.创建测试数据后,将从功能文件中运行场景.

  1. I have a couple of .feature files. I want to create test data that would be common to all of these feature files. Once the test data is created the scenarios will be run from the feature files.

在创建测试数据后,我还希望获得一些信息.例如,我创建的数据的ID.因此,我可以使用此信息来调用api,在我的方案中添加有效载荷.

I also want some info back after the test data is created. eg., ids of the data that i have created. So i can use this info to call the api's, add in payloads in my scenarios.

我认为我们可以通过以下方法做到这一点: 1.创建一个junit java文件.我在其中使用@BeforeClass定义了一个静态方法,并使用Karate的Runner()运行我的create-test-data.feature文件(我可以使用Karate击中应用程序api来创建一些数据).我在类型为Object的java类中定义一个属性,并将其设置为Runner.runFeature()的结果.

I think we could do this by: 1. Creating a junit java file. I define a static method with @BeforeClass in there and use Karate's runner() to run my create-test-data.feature file (I can use Karate to hit application api to create some data). I define a property in my java class of type Object and set it with the result of Runner.runFeature().

  1. 然后,我创建一个单独的功能文件test-data-details.feature.我在这里定义我的Java Interop代码.例如

  1. Then I create a separate feature file test-data-details.feature. I define my Java Interop code here. eg.,

  • def test_data = " var JavaOutput = Java.type('com.mycompany.JavaFile'); var testData = JavaOutput.propertyName; ""
  • def test_data = """ var JavaOutput = Java.type('com.mycompany.JavaFile'); var testData = JavaOutput.propertyName; """

现在,我的test-data-details.feature文件中有我的测试数据对象.我将具有测试方案的功能文件的背景"部分称为此.feature文件(callonce).因此,我可以重试ID,名称之类的测试数据详细信息.等,然后可以在api请求路径和有效负载中使用.

Now that I have my test data object in my test-data-details.feature file. I call this .feature file (callonce) in the Background section of my feature files that have test scenarios in. So I can retries the test data details like id, name. etc that I can then use in the api request paths and payloads.

我不确定以上设计是否是正确的方法.我尝试过,但是在我的Java文件中遇到了一些问题,下面的getClass()抱怨说它不能在静态方法中使用.

I am not sure if the above design is the correct way to go ahead. I tried but getting some issues in my Java file where getClass() below complains that it cannot be used in static method.

@RunWith(Karate.class)

公共类AccountRunner {

public class AccountRunner {

public static Object job = null;

@BeforeClass
public static void create_job(){
    Map<String, Object> result = Runner.runFeature(getClass(), "test-data.feature", null, true);
    job = result.get("job");
}

}

现在,以上所有内容都可能是完全错误的.需要有关如何在空手道中解决这种情况的帮助.

Now all of the above can be totally wrong. Need help on how to tackle this scenario in Karate.

谢谢

推荐答案

所以我已经实现了以下设计:

So i have implemented the following design:

我已经在TestRunner.java文件中创建了两个方法,一个带有BeforeClass,另一个带有AfterClass注释.在这些方法中,我可以调用特定的数据创建和清理功能文件,并将args作为Json对象传递.

I have created two methods one with BeforeClass and other with AfterClass annotation in my TestRunner.java file. In these methods I am able to call the specific data creation and clean-up feature files and pass args as Json object.

@RunWith(Karate.class)
@KarateOptions(tags = {"~@ignore"})
public class AccountRunner {

    public static Map<String, Object> result = null;

    @BeforeClass
    public static void create_job() throws IOException, ParseException {

        Class clazz = AccountRunner.class;
        URL file_loc = clazz.getResource("create-test-data-1.json");

        File file = new File(file_loc.getFile());

        JSONParser parser = new JSONParser();
        Object obj = parser.parse(new FileReader(file));
        JSONObject jsonObject = (JSONObject) obj;

        Map<String, Object> args = new HashMap();
        args.put("payload", jsonObject);

        result = Runner.runFeature(CommonFeatures.class, "create-data.feature", args, true);

    }


    @AfterClass
    public static void delete_investigation() {

        Map<String, Object> args = new HashMap();
        args.put("payload", result);

        Runner.runFeature(CommonFeatures.class, "delete-job.feature", args, true);
    }


}

要使用"mvn test"命令通过命令行运行这些测试,我已经对pom.xml进行了以下更改.

To run these tests via command line using "mvn test" command, i have done following changes in pom.xml.

           `<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <configuration>
                    <includes>
                        <include>**/*Runner.java</include>
                    </includes>
                </configuration>
            </plugin>`

使用此解决方案,我可以通过直接执行运行程序或通过命令行来在IDE中运行测试.但是,我没有遵循空手道建议的方法来找到运行所有测试的方法,在该方法中,我在测试套件级别具有* Test.java文件,并且将默认的maven配置与"mvn test"一起使用.该功能无法运行,因为在执行Runner文件之前调用了.feature文件,该文件具有创建测试数据的方法.

With this solution I able to run my tests in IDE by executing the runner directly or by command line. However, I have not found a way to run all of my tests by following the karate suggested approach where I have a *Test.java file at test suite level and I use default maven configuration with "mvn test". The features fails to run as the .feature file is called before the Runner file is executed which has method to create test data for tests.

也许有人可以提出其他建议,我可以使用空手道方法来运行* Test.java文件,而不是每个* Runner.java文件.

Maybe someone can suggest what else, I could do to use Karate approach of running *Test.java file instead of each *Runner.java file.

这篇关于为多个功能文件创建通用测试数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆