简体   繁体   English

如何基于外部输入运行Junit / TestNG测试?

[英]How to run Junit / TestNG tests based on an external input?

I have an Excel file where I am storing the Test case Name / Id. 我有一个Excel文件,用于存储测试用例名称/ ID。

Now, If my Test case name(in excel) is starting with 'test' I want to execute one of my Junit / TestNG test . 现在,如果我的测试用例名称(在excel中)以'test'开头,我想执行我的Junit / TestNG测试之一。 If my test case name(in excel) is starting with 'check', I want to execute another Junit / TestNG test. 如果我的测试用例名称(在excel中)以“ check”开头,则我想执行另一个Junit / TestNG测试。 And similarly, I want to execute different tests based on Ids in the excel. 同样,我想基于Excel中的Ids执行不同的测试。

How would I be able to do this? 我将如何做到这一点? Can some one please post an example which can be of great help to me. 可以请一个例子举个例子,对我有很大的帮助。

Thanks, Mike 谢谢,迈克

I think what you want to do is dynamically build tests based on your excel sheet. 我认为您想要做的是根据您的Excel工作表动态构建测试。 This is how you can run testng programmatically . 这就是您可以以编程方式运行testng的方式。

What you would need to do is read from your excel, create XmlClass and set the methods for xmlclass based on the testnames of your methods. 您需要做的是从Excel中读取信息,创建XmlClass并根据方法的测试名称设置xmlclass的方法。 Add the xmlclass to XmlSuite. 将xmlclass添加到XmlSuite。 So basically you are generating a dynamic suite at runtime based on the testcase names in your excel. 因此,基本上,您是在运行时根据excel中的测试用例名称生成一个动态套件。

What you are looking for is something called reflection. 您正在寻找的是一种叫做反射的东西。 In your xml you will have the class and method name of what you want to run. 在xml中,您将具有要运行的类和方法的名称。 In java you'll then use reflection to grab the class, and iterate over the methods to determine if any match what you want. 然后,在Java中,您将使用反射来获取类,并遍历方法以确定是否与您想要的匹配。 Reflection is generally not an efficient way of doing things, but for the purpose of testing it can do what you want. 反射通常不是一种有效的处理方式,但出于测试目的,它可以执行您想要的操作。

An example would be 一个例子是

for(Method method : this.getClass().getMethods()){
{
    if(method.getName().equals(name)){
         method.invoke();
    }
}

Please read http://docs.oracle.com/javase/tutorial/reflect/class/index.html for details. 有关详细信息,请阅读http://docs.oracle.com/javase/tutorial/reflect/class/index.html

I did something similar using testNG. 我使用testNG做过类似的事情。 My requirement was to run the test case which a user will select from a list of test cases. 我的要求是运行用户将从测试用例列表中选择的测试用例。 I was using Jenkins instead of excel sheet to let user select the test case from the list. 我使用的是Jenkins而不是excel表,以便用户从列表中选择测试用例。 Logic might work for you as well. 逻辑也可能对您有用。 Here is it: 就这个:

Rather than figuring out which method to run, I gave a unique test group name to all test cases. 我没有弄清楚要运行哪种方法,而是为所有测试用例指定了唯一的测试组名称。 This group name should be same as what you have in excel sheet. 该组名称应与excel工作表中的名称相同。 The only logic required would be to create the testng.xml to run the tests with the input group name. 唯一需要的逻辑是创建testng.xml以使用输入组名称运行测试。 This can be done using testNG objects or by creating an tesNG.xml using dom4j or something similar. 这可以使用testNG对象或通过使用dom4j或类似的东西创建tesNG.xml来完成。

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

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