Windows automation for the Java testng Framework-Autorun testng program Next

Source: Internet
Author: User
Tags testng

This article is intended to give readers a simple understanding of testng's automatic operation

Next https://www.cnblogs.com/xuezhezlr/p/9213456.html, the article roughly testng in the more specific two XML form, the reader can think, the ant of the XML code, Because it only controls the XML path of packaging and specifying testng, and then obtains the execution result to show the method, and TestNG's XML directly controls the order of running code, package class parameters, directly control the operation of the process, so, if the project subject change is not small, Only need to transform the XML of testng, so as to control the method of execution, achieve different testing purposes

So this article is also, the above-mentioned ant XML will not change, mainly on the XML in TestNG and the corresponding class and execution results, one after another to solve the problem mentioned above

1, if the system is required to execute only a portion of the code, such as executing a query interface, or just the insertion interface, how to do

2, how to make each class, even each method, in a certain way priority to execute

3, how to deal with the dependency problem of the method

4, if you need one or more variables (such as cookies), how to transfer in the code, so that today and tomorrow make all the different parameters can be used

The above issues can also be resolved in JUnit, the solution in testng more elegant and elegant a bit ~, the following two files are prepared or the original project, one is Zlr.java

Package ZLR;

Import Org.testng.annotations.Test;

@Test (groups = {"Group-class"})
public class ZLR {
@Test (groups = {"Group-a", "group-b"},priority = 2)
public void Cccmethod () {
System.out.println ("Method---CCC");
}

@Test (groups = {"Group-b"}, Priority = 3)
public void Bbbmethod () {
System.out.println ("Method---bbb");
}

@Test (groups = {"Group-b"},priority = 2)
public void Dddmethod () {
System.out.println ("Method---ddd");
}
@Test (groups = {"ZLR"})
public void Zlrmethod () {
System.out.println ("Method---ddd");
}
@Test (groups = {"Group-a"},priority = 3)
public void Aaamethod () {
System.out.println ("Method---aaa");
}
@Test (Dependsonmethods = {"Aaamethod"})
public void Method1 () {
System.out.println ("ZLR");
}
@Test (groups = {"Init"},priority = 3)
public void Xmethod () {
System.out.println ("Method---aaa");
}
@Test (groups = {"Group-c"},priority = 3)
public void Cmethod () {
System.out.println ("Method---c");
}
@Test (groups = {"Init"},priority = 3)
public void Ymethod () {
System.out.println ("Method---aaa");
}
@Test (groups = {"ss"},dependsongroups = {"Group-b", "Group-a"},alwaysrun=true)
public void Method3 () {
System.out.println ("ZLR");
}
@Test (dependsongroups = {"Init"})
public void Method2 () {
System.out.println ("ZLR");
}
}
One is Zlr2.java.
 package ZLR; 

Import org.testng.annotations.Parameters;
Import Org.testng.annotations.Test;

/**
* Created by Zouleiran to 2018/6/22.
*/
public class ZLR2 {
@Test (groups = {"Functest", "ch Eckintest "})
public void TestMethod1 () {
System.err.println (" groups = {functest, checkintest} ");


@Test (groups = {"Functest", "Checkintest"})
public void TestMethod2 () {
SYSTEM.ERR.PR Intln ("groups = {functest, checkintest}");
}

@Test (groups = {"Functest"})
public void testMethod3 () {
System.err.println ("Grou PS = {functest} ");
}
@Parameters ({"URL", "PORT"})
@Test (groups = {"Checkintest"})
public void testMethod4 (Strin G url,string PORT) {
System.err.println ("groups = {checkintest}" +url+port);
}
}
First of all, if you want to execute the entire class, you can use the XML as a previous blog, which is performed in class, and TestNG supports the method and code execution with group and method dimensions.

First, TestNG declares a thing called group, which we can think of as a group, the group can be a query group, can make the Insert group, or it can be another group, the following use ZLR2 that Java file demo, in ZLR2 we see there are 4 test class, Each of the following is marked with (groups = {"Checkintest"}) and (groups = {"Functest"}) can be considered to be divided into two large groups of all test, one group is checkintest another group is functest, A test can belong to a group or multiple groups, such as the TESTMETHOD4 in the above is the Checkintest group method, and then you can write down the following XML

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" >
<suite name= "Suite1" verbose= "1" >
<test name= "Regression1" >

<groups>
<run>
<include name = "Functest"/>
<exclude name= "Checkintest"/>
</run>
</groups>
<classes>
<class name = "ZLR.ZLR2"/>
</classes>
</test>
</suite>
Execution results are


You can see that the difference with the previous one is that there is more XML
<groups>
<run>
<include name = "Functest"/>
<exclude name= "Checkintest"/>
</run>
</groups>
These two lines mean that the group code that executes group functest instead of Checkintest is executed, which belongs to Functest code, even if a class belongs to both.

Second, the existence of testng can set the priority, so that the code is executed according to the priority, you can also use XML to control the order of execution

XML can describe the method of execution, if we write the following code in XML

<suite name= "Suite1"    verbose= "1" >
<test name= "Regression1" >

<classes>
<class name= "ZLR.ZLR" >
<methods>
<include name= "Aaamethod"/>
<include name= "Bbbmethod"/>
</methods>
</class>
</classes>
</test>
</suite>
The code executes the Aaamethod in the execution Bbbmethod, which, in turn, writes such
<include name= "Bbbmethod"/>
<include name= "Bbbmethod"/>
The code executes Bbbmethod in execution Aaamethod to control the code

On the other hand, in the code can also be @Test (priority = 3) of the way to describe the precedence, such as a method first = 2, and the other priorities = 3, then if you need to execute in the XML, you will also perform the top = 2 of the high-priority approach (if there is no reputation priority, as the lowest priority, that is, the first implementation of a priority method), such as in the XML to write the following code

<suite name= "Suite1"    verbose= "1" >
<test name= "Regression1" >

<classes>
<class name= "ZLR.ZLR" >
<methods>
<include name= "Aaamethod"/>
<include name= "Dddmethod"/>
</methods>
</class>
</classes>
</test>
</suite> code will execute Dddmethod first

Three, testng there is a dependency function way to let code execution, the way is

@Test (groups = {"ss"},dependsongroups  = {"Group-b", "Group-a"}) add parameters to Test so that this method relies on additional groups A and B to execute, In the actual execution, a and then B will be executed, and then the execution of this method
Dependencies can also depend on method dimensions, so that dependsongroups needs to write Dependsonmethods
To tell the truth in the actual use of this rate is not high, because there are before and more front beforeclass, even can be a method to write to the code inside, so rely on things not long use, the following is the XML of the instance
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Suite SYSTEM "Http://testng.org/testng-1.0.dtd" >
<suite name= "Suite1" verbose= "1" >
<test name= "Regression1" >

<groups>
<run>
<include name = "ss"/>
</run>
</groups>
<classes>
<class name = "ZLR.ZLR"/>
</classes>
</test>
</suite>

Four, this is I think testng very good point ~ ~ ~ really great, testng there is a way, so that users can be in the testng XML file and directly into the code, but there are drawbacks, if you forget to pass,,, the code will directly ignore this method

Specifically, this is

@Parameters ({"URL", "PORT"})
@Test (groups = {"Checkintest"})
public void TestMethod4 (String url,string PORT) {
System.err.println ("groups = {checkintest}" +url+port);
}
Many variables are known before the test method, which can be passed in as parameters in the method and used in the XML, which is then passed in the XML as follows
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Suite SYSTEM "Http://beust.com/testng/testng-1.0.dtd" >
<suite name= "Suite1" >
<test name= "Test1" >
<!--write the parameters to pass in the parameter tag--
<parameter name= "URL" value= "https://www.baidu.com" ></parameter>
<parameter name= "PORT" value= "443" ></parameter>
<groups>
<run>
<include name = "Checkintest"/>
            </run>
</groups>
<classes>
<class name= "ZLR.ZLR2"/>
</classes>
</test>
</suite>
Run results


You can see that the URL and port in the code have actually been passed in, and if the code does not pass in the XML, it will say that because the two parameters are not passed, this method does not run, ignoring to handle

Windows automation for the Java testng Framework-Autorun testng program Next

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.