举例一个接口测试的常见流程
1) 发送接口请求2) 断言接口响应状态是不是200 OK3) 断言接口的响应时间低于某一个值(看情况,不是必选)4) 断言响应数据是否正确,一般的做法是判断某一个值是否相等或者包含关系
我主要接触的都是json格式返回体
只要加一个JSONObject即可,通过对返回体中的值和预期值做断言,
若断言全部通过,则用例pass
若断言有一个未通过,则用例失败
补充一下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