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

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. pycharm进行调试[转载]

    转自:https://blog.csdn.net/william_hehe/article/details/80898031 1.首先设置断点. 2.Step into(F7):进入 若函数A内存在子 ...

  2. openstack 部署笔记--keystone

    控制节点 安装keystone包 # yum install openstack-keystone httpd mod_wsgi keystone配置文件 # vim /etc/keystone/ke ...

  3. [LeetCode] 717. 1-bit and 2-bit Characters_Easy

    We have two special characters. The first character can be represented by one bit 0. The second char ...

  4. Catch all the latest Jordan Release Dates

    In case y'all missed yesterday's news, Air Jordan 13 Olive 2018 officially unveiled their 2017 Holid ...

  5. Integer类之成员变量

    一.一共11个成员变量. 二.详情介绍. 1.value值.这个是Integer类的唯一标志.最重要的实例属性. 2.最小值和最大值常量.注意,计算机里面是以补码形式保存的,因此用十六进制时,给的数据 ...

  6. hexo修改Next主题的样式

    Next主题默认对超链接只有下划线样式,很容易被忽略,就想着怎么修改下 主题样式是在\hexoBlog\themes\next\source\css,这里面保存了Muse,Mist和Pisces三个主 ...

  7. 人生苦短之我用Python篇(socket编程)

    socket模块 实现简单的远程连接: 客户端: import socket client = socket.socket()#声明socket类型,同时生成socke连接t对象 client.con ...

  8. MFC工具栏的创建、设计与使用实例

    本文通过实例说明MFC工具栏的创建.设计和使用方法,包括三个demo.       demo1:创建一个工具栏 C++代码 //摘抄自MSDN demo1 (创建一个工具栏) 1.Create a t ...

  9. isKindOfClass和isMemberOfClass 的区别

    判断对象类型 -(BOOL) isKindOfClass: classObj判断是否是这个类或者这个类的子类的实例 -(BOOL) isMemberOfClass: classObj 判断是否是这个类 ...

  10. python 文件操作 练习:把一个目录下的所有文件名,打印一下,不要包含后缀名

    #coding=utf-8 import osos.chdir('d:\\test2')file_list=os.listdir('.')print "file_list:",fi ...