通用请求
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
@Test
public void testBaidu(){
Map<String, Object> data=new HashMap<>();
data.put("x", 1);
data.put("y", "xxxx");
given()
.contentType(ContentType.JSON)
.body(data)
.proxy("127.0.0.1", 7778)
.when()
.post("http://www.baidu.com/s")
.then()
.statusCode(200);
}
Filter机制
@Test
public void testBase64(){
given()
.auth().basic("hogwarts", "123456")
.log().all()
.filter( (req, res, ctx)->{
Response responseOri=ctx.next(req, res);
ResponseBuilder responseBuilder=new ResponseBuilder().clone(responseOri);
responseBuilder.setBody(
Base64.getDecoder().decode(
responseOri.getBody().asString().trim().replace("\\n", "")));
responseBuilder.setContentType(ContentType.JSON);
return responseBuilder.build();
})
.when().get("http://shell.testing-studio.com:9002/sec.json")
.then().log().all()
.statusCode(200).body("topics.id[0]", equalTo(15150));
}
数据驱动
CSV数据驱动
package online6; /**
* Created by seveniruby on 2017/3/21.
*/
import com.fasterxml.jackson.databind.MappingIterator;
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
@RunWith(Parameterized.class)
public class DataDriverByExcel {
@Parameterized.Parameters()
public static List<DataClass> data() throws IOException {
ArrayList<DataClass> data=new ArrayList<DataClass>();
CsvMapper mapper = new CsvMapper();
CsvSchema schema = mapper.schemaFor(DataClass.class);
//mapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
File csvFile = new File(DataDriverByExcel.class.getResource("../data/input.csv").getFile()); // or from String, URL etc
MappingIterator<DataClass> it = mapper.readerFor(DataClass.class).with(schema).readValues(csvFile);
while (it.hasNext()) {
DataClass row = it.next();
data.add(row);
}
return data;
}
@Parameterized.Parameter
public DataClass data;
@Test
public void demo() {
assertThat(data.getCount(), equalTo(Integer.parseInt(data.getKey())));
}
}
YAML数据驱动
package online6; /**
* Created by seveniruby on 2017/3/21.
*/
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.io.File;
import java.io.IOException;
import java.util.List;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;
@RunWith(Parameterized.class)
public class DataDriverByYaml {
@Parameterized.Parameters()
public static List<DataClass> data() throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
List<DataClass> data = mapper.readValue(
new File(DataDriverByYaml.class.getResource("../data/input.yaml").getFile()),
new TypeReference<List<DataClass>>(){}
);
return data;
}
@Parameterized.Parameter
public DataClass data;
@Test
public void demo() {
assertThat(data.getCount(), equalTo(Integer.parseInt(data.getKey())));
}
}
课后作业
利用SessionFilter或者自己定义的Filter实现Jenkins的登陆和带token触发job