Java Automation-data-driven Juint demo, previous

Source: Internet
Author: User
Tags assert

This article is intended to help readers introduce a general, fully automated code interface and simply describes how to use data driven for simple automation
After the introduction of the above several blog, I believe that the reader to automatically start the implementation of a Java compiled class has a certain understanding, but also fully capable to execute, so this article began to explain how to write a Java automation
First of all, the automation in the author's eyes, roughly divided into 2 kinds, 1 kinds of people need to control and input, for example, in the work of the need to change a designated order to complete the order, you need to enter a newly created order number, click on the execution of the code to complete the modification of order status, Use code to perform some of the work that someone should have done, this kind of automation in large companies are often various forms of automation platform, its essence is a semi-automatic, the second kind of automation refers to the more simple automation of less manual intervention, can be seen as fully automated, For example, some interfaces to run the Automation code to verify its correctness, this article is mainly about the latter, in order to lose the general, the majority of the author is the HTTP protocol, some large companies of the internal RPC interface, not discussed, but the difference is not small, this article is simply introduced
Most of our code is like this, as follows
Import Com.bs2.core.Util;
Import org.junit.*;
Import util. Pbgdict;
Import util. Pbgservice;

Import Java.util.HashMap;
Import Java.util.Map;

/**
* Created by ZLR on 16/10/8.
*/
public class Test_ylw_ip_ipcall_releasecall extends Pbgservice {
Public map<string, object> applyparams = null;
public void InitData () throws Exception {
Applyparams = new hashmap<string, object> ();
Applyparams.put (Pbgdict.token, TOKEN1);
Applyparams.put (Pbgdict.ip, "123.121.78.204");
Applyparams.put (Pbgdict.worktype, "unlimited");
Applyparams.put (Pbgdict.cat, "Youth, Martial arts");
Applyparams.put (pbgdict.anonymous, "0");
Applyparams.put (Pbgdict.projectdesc, "ZLR");
Applyparams.put (Pbgdict.demand, "network drama, TV series, variety Show");
Applyparams.put (pbgdict.identity, "ZLR" + util.strtime14 ());
Applyparams.put (Pbgdict.copybudget, "within 50,000");
Applyparams.put (Pbgdict.callend, "14766400001");
}
@Before
public void SetUp () throws Exception {
InitData ();
}
@After
public void TearDown () throws Exception {
This.applyParams.clear ();
This.applyparams = null;
}
@Test
public void Testssuccessreleasecall () throws exception{
Map<string,object> A=get1 ("/ip/ipcall/releasecall", Applyparams, "initiating the Papers", "0");
}
@Test
public void Testsfailreleasecall1 () throws exception{
Applyparams.remove (Pbgdict.token);
Get ("/ip/ipcall/releasecall", Applyparams, "No token", "3000");
}
}
The above is the author of the company before the automation code, the use of Java can be normal execution, the visible code is divided into the following sections
1before: This section places some initialization work, such as the initialization of some variables and maps, etc.
2test: The part that actually performs the test, which contains the execution and validation
3after: Data purge, mainly to clean the data in the database, the data of the variable is emptied, ready for the next execution
The visible code often has multiple test files in a class file, when someone proposes a data-driven
What is Data driven? Meaning refers to, because the code changes faster, parameter changes are relatively fast, so each class test will always be modified, so the code in the parameters from the code to extract, extract to the database or excle for unified management, more convenient, because now the test market always feel that No data-driven automation code is not a qualified automation code, so this article does not do data-driven is not necessary to discuss the first demonstration of data-driven, in the discussion
This article first rewrite the above code, become more simple and easier to discuss the code ideas, and then in the example, after the code is modified as follows
Package ZLR;

Import Org.junit.Assert;
Import Org.junit.Test;

public class Zlrshiyan {

@Test
public void Test1 () throws exception{
int a=1;
int b=2;
int c=3;
int d=6;
Assert.assertequals (A+b, C);
}
@Test
public void Test2 () throws exception{
int a=1;
int b=3;
int c=3;
int d=5;
Assert.assertequals (A+b, C);
}

}

Execution results

The above code is the original code conversion, the first success of the second will fail, and then we observed that, in fact, the variables only D and B, no changes to a and C, so the code can be used to optimize the juint before method, the following code

Package ZLR;

Import Org.junit.Assert;
Import Org.junit.Before;
Import Org.junit.Test;
public class Zlrshiyan {
private int A;
private int C;
@Before
public void Testinit () throws exception{
A=1;
c=3;
}
@Test
public void Test1 () throws exception{
int b=2;
int d=6;
Assert.assertequals (A+b+c, D);
}
@Test
public void Test2 () throws exception{
int b=3;
int d=5;
Assert.assertequals (A+b+c, D);
}
}
Here is a reminder, in the above code, A and C after the Declaration, in the before is given a value, then do not use the INT a=1 such statements, will make a 1 can not be passed to test and make them 0, no initialization successful

So if we're going to do data-driven, we're going to figure out how to extract the data, which is the parameter of the variable, and this code is like this.

Package ZLR;

Import Org.junit.Assert;
Import Org.junit.Before;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import org.junit.runners.Parameterized;

Import Java.util.Arrays;
Import java.util.Collection;

@RunWith (Parameterized.class)
public class Zlrshiyan {
private int A;
private int B;
private int C;
private int D;
@Parameterized. Parameters
@SuppressWarnings ("Unchecked")
public static Collection Zlrshiyan () {
Return arrays.aslist (New object[][]{{2,6},{3,5}});
}
Public Zlrshiyan (int b,int d) {
this.b = b;
This.d=d;
}
@Before
public void Testinit () throws exception{
A=1;
c=3;
}
@Test
public void Test1 () throws exception{
Assert.assertequals (A+b+c, D);
}
}
Execution effect

The above code is mainly to extract the parameters, you can see that the code put a,c in the before, each time will not change, and put B,d in the parameterization, the code will be directly declared

public static Collection Zlrshiyan () {
Return arrays.aslist (New object[][]{{2,6},{3,5}});
}
Use this method to declare a collection of parameters, using the
Public Zlrshiyan (int b,int d) {
this.b = b;
This.d=d;
}
method to accept the B,d two parameters, in fact, because Zlrshiyan and the class name is the same, so the code is considered to be initialized, will execute the test before executing the method, will be 2,6 as a whole into the method Zlrshiyan, then B as the first parameter will be taken 2, D takes 6 as the second argument, executes the subsequent assertion, and then, if
public static Collection Zlrshiyan () {
Return arrays.aslist (New object[][]{{2,6},{3,5}});
}
The parameter set in the inside also has the value, just like the code above, will also pass the 3,5, assigned to the b,d after the test, until the original parameter set is exhausted

The thought and code of parameterization are described above, although it is relatively simple, but the idea is very important. Next blog about how to extract code into documents such as Excle, and how to test the framework's approximate shape

Java Automation-data-driven Juint demo, previous

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.