举例一个接口测试的常见流程

1) 发送接口请求
2) 断言接口响应状态是不是200 OK
3) 断言接口的响应时间低于某一个值(看情况,不是必选)
4) 断言响应数据是否正确,一般的做法是判断某一个值是否相等或者包含关系

我主要接触的都是json格式返回体

只要加一个JSONObject即可,通过对返回体中的值和预期值做断言,

若断言全部通过,则用例pass

若断言有一个未通过,则用例失败

https://github.com/JasmineQian/API_JSON_Java.git

补充一下testNG中断言的用法

Assert.assertEquals 各种数据格式类型

public static void assertEquals(int actual, int expected, String message) {
assertEquals((Object)actual, (Object)expected, message);
}
public static void assertEquals(int actual, int expected) {
assertEquals((int)actual, (int)expected, (String)null);
}

源码如下

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package org.testng; import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import org.testng.collections.Lists;
import org.testng.internal.EclipseInterface; public class Assert {
protected Assert() {
} public static void assertTrue(boolean condition, String message) {
if (!condition) {
failNotEquals(condition, Boolean.TRUE, message);
} } public static void assertTrue(boolean condition) {
assertTrue(condition, (String)null);
} public static void assertFalse(boolean condition, String message) {
if (condition) {
failNotEquals(condition, Boolean.FALSE, message);
} } public static void assertFalse(boolean condition) {
assertFalse(condition, (String)null);
} public static void fail(String message, Throwable realCause) {
AssertionError ae = new AssertionError(message);
ae.initCause(realCause);
throw ae;
} public static void fail(String message) {
throw new AssertionError(message);
} public static void fail() {
fail((String)null);
} public static void assertEquals(Object actual, Object expected, String message) {
if (expected != null || actual != null) {
if (expected != null) {
if (expected.getClass().isArray()) {
assertArrayEquals(actual, expected, message);
return;
} if (expected.equals(actual)) {
return;
}
} failNotEquals(actual, expected, message);
}
} private static void assertArrayEquals(Object actual, Object expected, String message) {
if (actual.getClass().isArray()) {
int expectedLength = Array.getLength(expected);
if (expectedLength == Array.getLength(actual)) {
for(int i = 0; i < expectedLength; ++i) {
Object _actual = Array.get(actual, i);
Object _expected = Array.get(expected, i); try {
assertEquals(_actual, _expected);
} catch (AssertionError var8) {
failNotEquals(actual, expected, message == null ? "" : message + " (values as index " + i + " are not the same)");
}
} return;
} failNotEquals(Array.getLength(actual), expectedLength, message == null ? "" : message + " (Array lengths are not the same)");
} failNotEquals(actual, expected, message);
} public static void assertEquals(Object actual, Object expected) {
assertEquals((Object)actual, (Object)expected, (String)null);
} public static void assertEquals(String actual, String expected, String message) {
assertEquals((Object)actual, (Object)expected, message);
} public static void assertEquals(String actual, String expected) {
assertEquals((String)actual, (String)expected, (String)null);
} public static void assertEquals(double actual, double expected, double delta, String message) {
if (Double.isInfinite(expected)) {
if (expected != actual) {
failNotEquals(new Double(actual), new Double(expected), message);
}
} else if (Math.abs(expected - actual) > delta) {
failNotEquals(new Double(actual), new Double(expected), message);
} } public static void assertEquals(double actual, double expected, double delta) {
assertEquals(actual, expected, delta, (String)null);
} public static void assertEquals(float actual, float expected, float delta, String message) {
if (Float.isInfinite(expected)) {
if (expected != actual) {
failNotEquals(new Float(actual), new Float(expected), message);
}
} else if (Math.abs(expected - actual) > delta) {
failNotEquals(new Float(actual), new Float(expected), message);
} } public static void assertEquals(float actual, float expected, float delta) {
assertEquals(actual, expected, delta, (String)null);
} public static void assertEquals(long actual, long expected, String message) {
assertEquals((Object)actual, (Object)expected, message);
} public static void assertEquals(long actual, long expected) {
assertEquals(actual, expected, (String)null);
} public static void assertEquals(boolean actual, boolean expected, String message) {
assertEquals((Object)actual, (Object)expected, message);
} public static void assertEquals(boolean actual, boolean expected) {
assertEquals(actual, expected, (String)null);
} public static void assertEquals(byte actual, byte expected, String message) {
assertEquals((Object)actual, (Object)expected, message);
} public static void assertEquals(byte actual, byte expected) {
assertEquals((byte)actual, (byte)expected, (String)null);
} public static void assertEquals(char actual, char expected, String message) {
assertEquals((Object)actual, (Object)expected, message);
} public static void assertEquals(char actual, char expected) {
assertEquals((char)actual, (char)expected, (String)null);
} public static void assertEquals(short actual, short expected, String message) {
assertEquals((Object)actual, (Object)expected, message);
} public static void assertEquals(short actual, short expected) {
assertEquals((short)actual, (short)expected, (String)null);
} public static void assertEquals(int actual, int expected, String message) {
assertEquals((Object)actual, (Object)expected, message);
} public static void assertEquals(int actual, int expected) {
assertEquals((int)actual, (int)expected, (String)null);
} public static void assertNotNull(Object object) {
assertNotNull(object, (String)null);
} public static void assertNotNull(Object object, String message) {
if (object == null) {
String formatted = "";
if (message != null) {
formatted = message + " ";
} fail(formatted + "expected object to not be null");
} assertTrue(object != null, message);
} public static void assertNull(Object object) {
assertNull(object, (String)null);
} public static void assertNull(Object object, String message) {
if (object != null) {
failNotSame(object, (Object)null, message);
} } public static void assertSame(Object actual, Object expected, String message) {
if (expected != actual) {
failNotSame(actual, expected, message);
}
} public static void assertSame(Object actual, Object expected) {
assertSame(actual, expected, (String)null);
} public static void assertNotSame(Object actual, Object expected, String message) {
if (expected == actual) {
failSame(actual, expected, message);
} } public static void assertNotSame(Object actual, Object expected) {
assertNotSame(actual, expected, (String)null);
} private static void failSame(Object actual, Object expected, String message) {
String formatted = "";
if (message != null) {
formatted = message + " ";
} fail(formatted + EclipseInterface.ASSERT_LEFT2 + expected + EclipseInterface.ASSERT_MIDDLE + actual + EclipseInterface.ASSERT_RIGHT);
} private static void failNotSame(Object actual, Object expected, String message) {
String formatted = "";
if (message != null) {
formatted = message + " ";
} fail(formatted + EclipseInterface.ASSERT_LEFT + expected + EclipseInterface.ASSERT_MIDDLE + actual + EclipseInterface.ASSERT_RIGHT);
} private static void failNotEquals(Object actual, Object expected, String message) {
fail(format(actual, expected, message));
} static String format(Object actual, Object expected, String message) {
String formatted = "";
if (null != message) {
formatted = message + " ";
} return formatted + EclipseInterface.ASSERT_LEFT + expected + EclipseInterface.ASSERT_MIDDLE + actual + EclipseInterface.ASSERT_RIGHT;
} public static void assertEquals(Collection<?> actual, Collection<?> expected) {
assertEquals((Collection)actual, (Collection)expected, (String)null);
} public static void assertEquals(Collection<?> actual, Collection<?> expected, String message) {
if (actual != expected) {
if (actual == null || expected == null) {
if (message != null) {
fail(message);
} else {
fail("Collections not equal: expected: " + expected + " and actual: " + actual);
}
} assertEquals(actual.size(), expected.size(), message + ": lists don't have the same size");
Iterator<?> actIt = actual.iterator();
Iterator<?> expIt = expected.iterator();
int i = -1; while(actIt.hasNext() && expIt.hasNext()) {
++i;
Object e = expIt.next();
Object a = actIt.next();
String explanation = "Lists differ at element [" + i + "]: " + e + " != " + a;
String errorMessage = message == null ? explanation : message + ": " + explanation;
assertEquals(a, e, errorMessage);
} }
} public static void assertEquals(Iterator<?> actual, Iterator<?> expected) {
assertEquals((Iterator)actual, (Iterator)expected, (String)null);
} public static void assertEquals(Iterator<?> actual, Iterator<?> expected, String message) {
if (actual != expected) {
if (actual == null || expected == null) {
if (message != null) {
fail(message);
} else {
fail("Iterators not equal: expected: " + expected + " and actual: " + actual);
}
} int i = -1; while(actual.hasNext() && expected.hasNext()) {
++i;
Object e = expected.next();
Object a = actual.next();
String explanation = "Iterators differ at element [" + i + "]: " + e + " != " + a;
String errorMessage = message == null ? explanation : message + ": " + explanation;
assertEquals(a, e, errorMessage);
} String explanation;
String errorMessage;
if (actual.hasNext()) {
explanation = "Actual iterator returned more elements than the expected iterator.";
errorMessage = message == null ? explanation : message + ": " + explanation;
fail(errorMessage);
} else if (expected.hasNext()) {
explanation = "Expected iterator returned more elements than the actual iterator.";
errorMessage = message == null ? explanation : message + ": " + explanation;
fail(errorMessage);
} }
} public static void assertEquals(Iterable<?> actual, Iterable<?> expected) {
assertEquals((Iterable)actual, (Iterable)expected, (String)null);
} public static void assertEquals(Iterable<?> actual, Iterable<?> expected, String message) {
if (actual != expected) {
if (actual == null || expected == null) {
if (message != null) {
fail(message);
} else {
fail("Iterables not equal: expected: " + expected + " and actual: " + actual);
}
} Iterator<?> actIt = actual.iterator();
Iterator<?> expIt = expected.iterator();
assertEquals(actIt, expIt, message);
}
} public static void assertEquals(Object[] actual, Object[] expected, String message) {
if (actual != expected) {
if (actual == null && expected != null || actual != null && expected == null) {
if (message != null) {
fail(message);
} else {
fail("Arrays not equal: " + Arrays.toString(expected) + " and " + Arrays.toString(actual));
}
} assertEquals((Collection)Arrays.asList(actual), (Collection)Arrays.asList(expected), message);
}
} public static void assertEqualsNoOrder(Object[] actual, Object[] expected, String message) {
if (actual != expected) {
if (actual == null && expected != null || actual != null && expected == null) {
failAssertNoEqual("Arrays not equal: " + Arrays.toString(expected) + " and " + Arrays.toString(actual), message);
} if (actual.length != expected.length) {
failAssertNoEqual("Arrays do not have the same size:" + actual.length + " != " + expected.length, message);
} List<Object> actualCollection = Lists.newArrayList();
Object[] arr$ = actual;
int len$ = actual.length; int i$;
Object o;
for(i$ = 0; i$ < len$; ++i$) {
o = arr$[i$];
actualCollection.add(o);
} arr$ = expected;
len$ = expected.length; for(i$ = 0; i$ < len$; ++i$) {
o = arr$[i$];
actualCollection.remove(o);
} if (actualCollection.size() != 0) {
failAssertNoEqual("Arrays not equal: " + Arrays.toString(expected) + " and " + Arrays.toString(actual), message);
} }
} private static void failAssertNoEqual(String defaultMessage, String message) {
if (message != null) {
fail(message);
} else {
fail(defaultMessage);
} } public static void assertEquals(Object[] actual, Object[] expected) {
assertEquals((Object[])actual, (Object[])expected, (String)null);
} public static void assertEqualsNoOrder(Object[] actual, Object[] expected) {
assertEqualsNoOrder(actual, expected, (String)null);
} public static void assertEquals(byte[] actual, byte[] expected) {
assertEquals(actual, expected, "");
} public static void assertEquals(byte[] actual, byte[] expected, String message) {
if (expected != actual) {
if (null == expected) {
fail("expected a null array, but not null found. " + message);
} if (null == actual) {
fail("expected not null array, but null found. " + message);
} assertEquals(actual.length, expected.length, "arrays don't have the same size. " + message); for(int i = 0; i < expected.length; ++i) {
if (expected[i] != actual[i]) {
fail("arrays differ firstly at element [" + i + "]; " + "expected value is <" + expected[i] + "> but was <" + actual[i] + ">. " + message);
}
} }
} public static void assertEquals(Set<?> actual, Set<?> expected) {
assertEquals((Set)actual, (Set)expected, (String)null);
} public static void assertEquals(Set<?> actual, Set<?> expected, String message) {
if (actual != expected) {
if (actual == null || expected == null) {
if (message == null) {
fail("Sets not equal: expected: " + expected + " and actual: " + actual);
} else {
failNotEquals(actual, expected, message);
}
} if (!actual.equals(expected)) {
if (message == null) {
fail("Sets differ: expected " + expected + " but got " + actual);
} else {
failNotEquals(actual, expected, message);
}
} }
} public static void assertEquals(Map<?, ?> actual, Map<?, ?> expected) {
if (actual != expected) {
if (actual == null || expected == null) {
fail("Maps not equal: expected: " + expected + " and actual: " + actual);
} if (actual.size() != expected.size()) {
fail("Maps do not have the same size:" + actual.size() + " != " + expected.size());
} Set<?> entrySet = actual.entrySet();
Iterator iterator = entrySet.iterator(); while(iterator.hasNext()) {
Entry<?, ?> entry = (Entry)iterator.next();
Object key = entry.getKey();
Object value = entry.getValue();
Object expectedValue = expected.get(key);
assertEquals(value, expectedValue, "Maps do not match for key:" + key + " actual:" + value + " expected:" + expectedValue);
} }
} public static void assertNotEquals(Object actual1, Object actual2, String message) {
boolean fail = false; try {
assertEquals(actual1, actual2);
fail = true;
} catch (AssertionError var5) {
;
} if (fail) {
fail(message);
} } public static void assertNotEquals(Object actual1, Object actual2) {
assertNotEquals((Object)actual1, (Object)actual2, (String)null);
} static void assertNotEquals(String actual1, String actual2, String message) {
assertNotEquals((Object)actual1, (Object)actual2, message);
} static void assertNotEquals(String actual1, String actual2) {
assertNotEquals((String)actual1, (String)actual2, (String)null);
} static void assertNotEquals(long actual1, long actual2, String message) {
assertNotEquals((Object)actual1, (Object)actual2, message);
} static void assertNotEquals(long actual1, long actual2) {
assertNotEquals(actual1, actual2, (String)null);
} static void assertNotEquals(boolean actual1, boolean actual2, String message) {
assertNotEquals((Object)actual1, (Object)actual2, message);
} static void assertNotEquals(boolean actual1, boolean actual2) {
assertNotEquals(actual1, actual2, (String)null);
} static void assertNotEquals(byte actual1, byte actual2, String message) {
assertNotEquals((Object)actual1, (Object)actual2, message);
} static void assertNotEquals(byte actual1, byte actual2) {
assertNotEquals((byte)actual1, (byte)actual2, (String)null);
} static void assertNotEquals(char actual1, char actual2, String message) {
assertNotEquals((Object)actual1, (Object)actual2, message);
} static void assertNotEquals(char actual1, char actual2) {
assertNotEquals((char)actual1, (char)actual2, (String)null);
} static void assertNotEquals(short actual1, short actual2, String message) {
assertNotEquals((Object)actual1, (Object)actual2, message);
} static void assertNotEquals(short actual1, short actual2) {
assertNotEquals((short)actual1, (short)actual2, (String)null);
} static void assertNotEquals(int actual1, int actual2, String message) {
assertNotEquals((Object)actual1, (Object)actual2, message);
} static void assertNotEquals(int actual1, int actual2) {
assertNotEquals((int)actual1, (int)actual2, (String)null);
} public static void assertNotEquals(float actual1, float actual2, float delta, String message) {
boolean fail = false; try {
assertEquals(actual1, actual2, delta, message);
fail = true;
} catch (AssertionError var6) {
;
} if (fail) {
fail(message);
} } public static void assertNotEquals(float actual1, float actual2, float delta) {
assertNotEquals(actual1, actual2, delta, (String)null);
} public static void assertNotEquals(double actual1, double actual2, double delta, String message) {
boolean fail = false; try {
assertEquals(actual1, actual2, delta, message);
fail = true;
} catch (AssertionError var9) {
;
} if (fail) {
fail(message);
} } public static void assertNotEquals(double actual1, double actual2, double delta) {
assertNotEquals(actual1, actual2, delta, (String)null);
}
}

  

API接口自动化之2 处理http请求的返回体,对返回体做校验的更多相关文章

  1. python接口自动化(十)--post请求四种传送正文方式(详解)

    简介 post请求我在python接口自动化(八)--发送post请求的接口(详解)已经讲过一部分了,主要是发送一些较长的数据,还有就是数据比较安全等.我们要知道post请求四种传送正文方式首先需要先 ...

  2. python+pytest接口自动化(4)-requests发送get请求

    python中用于请求http接口的有自带的urllib和第三方库requests,但 urllib 写法稍微有点繁琐,所以在进行接口自动化测试过程中,一般使用更为简洁且功能强大的 requests ...

  3. API接口自动化之1 常见的http请求

    常见的HTTP请求 就我所想到的,目前如果想做http/https请求接口自动户化,要处理的一些区别 get请求post请求 httphttps 单个值键值对键值对的搭配方式 POST请求体,后续讲一 ...

  4. API接口自动化之3 同一个war包中多个接口做自动化测试

    同一个war包中多个接口做自动化测试 一个接口用一个测试类,每个测试用例如下,比如下面是4个测试用例,每个详细的测试用例中含有请求入参,返回体校验,以此来判断每条测试用例是否通过 一个war包中,若含 ...

  5. python接口自动化一(发送get请求)

    一.环境安装 1.用pip安装requests模块 >>pip install requests 二.get请求 1.导入requests后,用get方法就能直接访问url地址,如:htt ...

  6. Python接口自动化【requests处理Token请求】

    首先说一下使用python模拟登录或注册时,对于带token的页面怎么登录注册模拟的思路: 1.对于带token的页面,需要先从最开始的页面获取合法token 2.然后使用获取到的合法token进行后 ...

  7. python接口自动化-发xml格式post请求

    前言 post请求相对于get请求多一个body部分,body部分常见的数据类型有以下四种(注意是常见的,并不是只有4种) application/x-www-form-urlencoded appl ...

  8. Jenkins 对项目持续集成的配置之二 API接口自动化 Ant+Jmeter

    先介绍一下Ant+Jmeter 略 我的另一篇文章有讲在linux上部署ant + jmeter以满足CI持续化集成 https://www.cnblogs.com/qianjinyan/p/9067 ...

  9. REST API 自动化测试 利器Rest Assured(API接口自动化测试框架体系)

    现在,越来越多的 Web 应用转向了 RESTful 的架构,很多产品和应用暴露给用户的往往就是一组 REST API,这样有一个好处,用户可以根据需要,调用不同的 API,整合出自己的应用出来.从这 ...

随机推荐

  1. PAT 1034 Head of a Gang[难][dfs]

    1034 Head of a Gang (30)(30 分) One way that the police finds the head of a gang is to check people's ...

  2. python接口测试中安装whl格式的requests第三方模块

    下载 安装 requests第三方模块 下载:http://docs.python-requests.org/en/latest/user/install/#install 我下载是 https:// ...

  3. 树莓派3Braspberry pi 如何汉化显示中文教程

    树莓派默认是采用英文字库的,而且系统里没有预装中文字库,所以即使你在locale中改成中文,也不会显示中文,只会显示一堆方块.因此需要我们手动来安装中文字体. 好在有一个中文字体是免费开源使用的.ss ...

  4. c++多个文件中如何共用一个全局变量

    例子: 头文件:state.h   源文件:state.cpp 其它源文件:t1.cpp  t2.cpp  t3.cpp, 这些源文件都包含头文件state.h. 需要定义一个全局变量供这些源文件中使 ...

  5. 下载YouTube视频的方法

    这个网站就可以: http://www.clipconverter.cc/ 更多的网站及介绍参考知乎:http://www.zhihu.com/question/19964181

  6. 我的sublime 插件配置

    一个插件就是一个软件 ,这就是sublime的理念 . 1.Packag control 给sublime配置插件当然少不了Package control ,首先安装 Package control ...

  7. <<Joint Deep Modeling of Users and Items Using Reviews for Recommendation>> 评论打分预测

    综述: 本文将 CNN 与 FM(Factorization Machine) 结合,基于评论文本来进行评分预测. 简介: 目前将神经网络应用推荐系统的研究工作中,有一类思路是把如CNN等神经网络作为 ...

  8. 33. Search in Rotated Sorted Array(二分查找)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  9. Java设计模式应用——工厂模式

    工厂模式有三种:简单工厂.工厂方法.抽象工厂 一. 抽象工厂 1. 一个可以生产多种产品的工厂: 2. 不改变工厂无法生产新的产品. package com.coshaho.learn.factory ...

  10. Applying the Kappa architecture in the telco industry

    https://www.oreilly.com/ideas/applying-the-kappa-architecture-in-the-telco-industry Kappa architectu ...