简体   繁体   English

TestNG Automation Framework分别在本地运行测试

[英]TestNG Automation Framework running tests individually, locally

I am a beginner in TestNG. 我是TestNG的初学者。 I am currently working in a company where they have a homegrown api automation framework. 我目前在一家拥有本地api自动化框架的公司中工作。 I am used to running my test case in isolation when local and using Cucumber. 在本地和使用Cucumber时,我习惯于独立运行测试用例。 However, things are very tightly coupled in this framework. 但是,在此框架中,事物紧密相连。 Let me give a briefing on different aspects of the framework: 让我简要介绍一下框架的各个方面:

Test Data Management: Test data are read through properties file. 测试数据管理:通过属性文件读取测试数据。 All the test case data are listed in the properties using key values per api module. 使用每个api模块的键值在属性中列出所有测试用例数据。 The way the keys are numbered are by testcase type and a counter attached to it. 密钥的编号方式取决于测试用例类型和附加的计数器。 So for example: 因此,例如:

example.properties

.....
.....
    getTestCase38=TC_ID_38
    getDescription38= Description_38
    getUserID38=<some value>
    getUserType38=<some value>
    expectedUserType38=<some value>

    getTestCase39=TC_ID_39
    getDescription39= Description_39
    getUserID39=<some value>
    getUserType39=<some value>
    expectedUserType39=<some value>

    postTestCase1=TC_ID_1
    postDescription1= Description_1
    postUserID1=<some value>
    postUserType1=<some value>
    expectedUserType1=<some value>
...
...

Test Data Flow: The data is read from the properties file. 测试数据流:从属性文件中读取数据。 A property file maps to a TestNG class. 属性文件映射到TestNG类。 The naming convention of these methods in a TestNG class are as follows: 这些方法在TestNG类中的命名约定如下:

public class ModuleA extends ModuleACommon{    

int getcount = 0;
int postcount = 0;

    @Test(priority=20)
    public void testMoudleAGetPositive38(){
     callMethodToReadData(getcount++);
     ....
    }   

    @Test(priority=21)
    public void testModuleAGetPositive39(){
     callMethodToReadData(getcount++);
     ....
     ....
    } 
    ......
    ......

    @Test(priority=30)
    public void testModuleAPostPositive1(){
     callMethodToReadData(postcount++);
     ....
     ....
    }   
   ......
   ......
}

Note that when callMethodToReadData is called the count is incremented and it is used to build the key to access the test data from the properties file. 请注意,在callMethodToReadData时,计数会增加,它用于构建键来访问属性文件中的测试数据。 The test cases are set to a priority in sequential order, this ensures that the proper key is constructed. 将测试用例按顺序设置为优先级,以确保构造正确的密钥。 This is painful because if I want to add a new test case following this pattern. 这很痛苦,因为如果我想按照此模式添加新的测试用例。 Lets say I want to add a test case and assign it a priority of 21 then I will have to adjust the priority for all the test methods after 21 including the 21st priority test case. 假设我要添加一个测试用例并为其分配优先级21,那么我将必须为21之后的所有测试方法(包括第21个优先级测试用例)调整优先级。

For short term: I am trying to find an easiest way to run one test in isolation. 短期而言:我正在尝试找到一种最简单的方法来单独运行一项测试。 There are dirty ways of accomplishing this. 有完成此目的的肮脏方法。 For example, I can just comment out other test case and reassign the appropriate count values but this is too inefficient. 例如,我可以注释掉其他测试用例并重新分配适当的计数值,但这效率太低。

Long Term: I would love to hear opinions and suggestions to improve this in the long run. 长期:我很想听听意见和建议,从长远来看可以改善这一点。 I have couple of ideas myself for long term improvements, however I was hoping to get more insightful suggestions from here. 我本人对长期改进有一些想法,但是我希望从这里获得更多有见地的建议。

Also, let me know if there are any hacks that could solve the problem of adding test cases and not having to reorganize the priorities (I have a solution that I have already implemented where I just use a unique key that is more descriptive of the actual test case). 另外,让我知道是否有任何骇客可以解决添加测试用例而不必重新组织优先级的问题(我已经实现了一个解决方案,其中我只使用了一个对实际情况更具描述性的唯一密钥测试用例)。

Consolidating some of the problems that can be seen with this approach : 合并使用此方法可以看到的一些问题:

  1. No support for parallel execution : The way your tests are built, if I were to attempt to run them in parallel, the entire system would collapse, because the method that fetches data from a data source (properties file in your case) is dependent on a non thread safe data member in the class. 不支持并行执行:测试的构建方式,如果我尝试并行运行它们,则整个系统将崩溃,因为从数据源(在您的情况下为属性文件)获取数据的方法取决于类中的非线程安全数据成员。
  2. As you already called out, cannot add new tests with ease nor have the flexibility to run them as standalone tests. 正如您已经喊过的,既不能轻松添加新测试,也不能灵活地将它们作为独立测试运行。
  3. Re-ordering of tests 重新测试顺序

In order to be able to run a test independently is to be able to change your key. 为了能够独立运行测试是可以更改您的密钥。 You key currently is relying on some int value, which needs to be passed off to the data source so that you can retrieve properties for it. 当前,您的密钥依赖于一些int值,该值需要传递给数据源,以便您可以检索其属性。

I would suggest that you change this to instead start being the method name of the @Test . 我建议您将其更改为开始使用@Test的方法名称。 Since you are saying that for every test class you would be having a properties file and since Java does not let you define two methods with the same name (of-course method overloading will let you do that, but since we are talking about @Test methods, we can conveniently ignore that part), your keys would still be unique if you had them depend on the method name. 既然您说的是每个测试类都有一个属性文件,并且Java不允许您使用相同的名称定义两个方法(当然,方法重载可以使您做到这一点,但是既然我们在谈论@Test方法,我们可以方便地忽略该部分),如果您的键取决于方法名称,则它们仍然是唯一的。 Now you don't need to be explicitly referring to the method name also. 现在,您也不需要显式地引用方法名称。 From within your @Test method, if you invoked callMethodToReadData() , then your callMethodToReadData() can easily retrieve the currently running @Test method's name by merely calling org.testng.Reporter.getCurrentTestResult().getMethod().getMethodName() (Remember that TestNG ensures that there's a valid ITestResult object when you query the thread local variable Reporter.getCurrentTestResult from within a @Test method) @Test方法中,如果调用callMethodToReadData() ,则callMethodToReadData()只需调用org.testng.Reporter.getCurrentTestResult().getMethod().getMethodName()即可轻松检索当前正在运行的@Test方法的名称org.testng.Reporter.getCurrentTestResult().getMethod().getMethodName()请记住,当您从@Test方法中查询线程局部变量Reporter.getCurrentTestResult时,TestNG确保存在有效的ITestResult对象。

This will now free you off from the dependency on a counter, to form your key. 现在,这将使您摆脱对计数器的依赖,从而形成密钥。 Your properties file is much more readable because the key now represents the method name, so its a lot more clear as to, to which test method does the data belong to. 您的属性文件更具可读性,因为键现在代表方法名称,因此对于数据所属的测试方法而言,它更加清晰。

You also don't have to alter priorities when adding a new test (of-course you would need to adjust priorities if you really want to change the order of the tests, because that is what priorities are for.. soft dependency ) 添加新测试时,您也不必更改优先级(当然,如果您确实想更改测试的顺序,则需要调整优先级,因为那是优先级。 soft dependency

You can get a bit more fancy by even building an implementation of org.testng.IAnnotationTransformer wherein you can enable/disable @Test methods at will based on a JVM argument which you can read. 您甚至可以通过构建org.testng.IAnnotationTransformer的实现来获得更多幻想,在该实现中,您可以基于可读的JVM参数随意启用/禁用@Test方法。 So that literally lets you run any @Test method at will. 这样就可以让您@Test运行任何@Test方法。 If you are using maven as your build tool, then you really dont need the transformer because maven surefire plugin by itself lets you do that. 如果您使用maven作为构建工具,那么您实际上不需要变压器,因为maven surefire插件本身就可以做到这一点。

In the long run, I believe you need to start looking at dataprovider and perhaps even factories. 从长远来看,我相信你需要开始寻找dataprovider ,甚至工厂。 You might also want to get away from the notion of properties and instead move over to something sophisticated such as JSON/YAML/XML or just fall back to excel spreadsheets, because in excel spreadsheets you can literally start creating a full fledged RDBMS by visualising each sheet as a table and then having one table refer to the other using keys. 您可能还希望摆脱属性的概念,而转而使用诸如JSON / YAML / XML之类的复杂功能,或者只是退回到excel电子表格中,因为在excel电子表格中,您可以通过可视化每个文字来真正地创建完整的RDBMS。表作为一个表,然后让一个表使用键引用另一个表。 You can then go about building a sophisticated excel data provider, which is capable of extracting data from the spreadsheet and populating a POJO (you would need to create one, which models your spreadsheet row of data). 然后,您可以开始构建高级的excel数据提供程序,该提供程序能够从电子表格中提取数据并填充POJO(您需要创建一个模型来对电子表格数据行进行建模)。 This is what we ended up building and open sourcing as part of building SeLion . 这就是我们构建SeLion的一部分,最终进行了构建和开放源代码 To know more about the excel data provider that I am talking about, you can refer here 要了解有关我正在谈论的excel数据提供程序的更多信息,可以在这里引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM