自动化测试-断言Assert类封装成断言失败时自动截图
// 重写Assert类,修改了fail方法,使抛异常前截图保存在当前日期文件夹下
package com.yyc.utils;
import com.google.common.collect.Lists;
import java.lang.reflect.Array;
import java.util.*;
public class Asserts {
public static final Character OPENING_CHARACTER = '[';
public static final Character CLOSING_CHARACTER = ']';
public static final String ASSERT_LEFT = "expected " + OPENING_CHARACTER;
public static final String ASSERT_LEFT2 = "expected not same " + OPENING_CHARACTER;
public static final String ASSERT_MIDDLE = CLOSING_CHARACTER + " but found " + OPENING_CHARACTER;
public static final String ASSERT_RIGHT = Character.toString(CLOSING_CHARACTER);
/**
* Protect constructor since it is a static only class
*/
protected Asserts() {
// hide constructor
}
/**
* Asserts that a condition is true. If it isn't,
* an AssertionError, with the given message, is thrown.
* @param condition the condition to evaluate
* @param message the assertion error message
*/
static public void assertTrue(boolean condition, String message) {
if(!condition) {
failNotEquals( Boolean.valueOf(condition), Boolean.TRUE, message);
}
}
/**
* Asserts that a condition is true. If it isn't,
* an AssertionError is thrown.
* @param condition the condition to evaluate
*/
static public void assertTrue(boolean condition) {
assertTrue(condition, null);
}
/**
* Asserts that a condition is false. If it isn't,
* an AssertionError, with the given message, is thrown.
* @param condition the condition to evaluate
* @param message the assertion error message
*/
static public void assertFalse(boolean condition, String message) {
if(condition) {
failNotEquals( Boolean.valueOf(condition), Boolean.FALSE, message); // TESTNG-81
}
}
/**
* Asserts that a condition is false. If it isn't,
* an AssertionError is thrown.
* @param condition the condition to evaluate
*/
static public void assertFalse(boolean condition) {
assertFalse(condition, null);
}
/**
* Fails a test with the given message and wrapping the original exception.
*
* @param message the assertion error message
* @param realCause the original exception
*/
static public void fail(String message, Throwable realCause) {
AssertionError ae = new AssertionError(message);
ae.initCause(realCause);
throw ae;
}
/**
* Fails a test with the given message.
* @param message the assertion error message
* 异常前截图
*/
static public void fail(String message) {
// 自动截图
Snapshot.snapshot(MathRandom.getRandName());
throw new AssertionError(message);
}
/**
* Fails a test with no message.
*/
static public void fail() {
fail(null);
}
/**
* Asserts that two objects are equal. If they are not,
* an AssertionError, with the given message, is thrown.
* @param actual the actual value
* @param expected the expected value
* @param message the assertion error message
*/
static public void assertEquals(Object actual, Object expected, String message) {
if((expected == null) && (actual == null)) {
return;
}
if(expected != null) {
if (expected.getClass().isArray()) {
assertArrayEquals(actual, expected, message);
return;
} else if (expected.equals(actual)) {
return;
}
}
failNotEquals(actual, expected, message);
}
/**
* Asserts that two objects are equal. It they are not, an AssertionError,
* with given message, is thrown.
* @param actual the actual value
* @param expected the expected value (should be an non-null array value)
* @param message the assertion error message
*/
private static void assertArrayEquals(Object actual, Object expected, String message) {
//is called only when expected is an array
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 ae) {
failNotEquals(actual, expected, message == null ? "" : message
+ " (values at index " + i + " are not the same)");
}
}
//array values matched
return;
} else {
failNotEquals(Array.getLength(actual), expectedLength, message == null ? "" : message
+ " (Array lengths are not the same)");
}
}
failNotEquals(actual, expected, message);
}
/**
* Asserts that two objects are equal. If they are not,
* an AssertionError is thrown.
* @param actual the actual value
* @param expected the expected value
*/
static public void assertEquals(Object actual, Object expected) {
assertEquals(actual, expected, null);
}
/**
* Asserts that two Strings are equal. If they are not,
* an AssertionError, with the given message, is thrown.
* @param actual the actual value
* @param expected the expected value
* @param message the assertion error message
*/
static public void assertEquals(String actual, String expected, String message) {
assertEquals((Object) actual, (Object) expected, message);
}
/**
* Asserts that two Strings are equal. If they are not,
* an AssertionError is thrown.
* @param actual the actual value
* @param expected the expected value
*/
static public void assertEquals(String actual, String expected) {
assertEquals(actual, expected, null);
}
/**
* Asserts that two doubles are equal concerning a delta. If they are not,
* an AssertionError, with the given message, is thrown. If the expected
* value is infinity then the delta value is ignored.
* @param actual the actual value
* @param expected the expected value
* @param delta the absolute tolerable difference between the actual and expected values
* @param message the assertion error message
*/
static public void assertEquals(double actual, double expected, double delta, String message) {
// handle infinity specially since subtracting to infinite values gives NaN and the
// the following test fails
if(Double.isInfinite(expected)) {
if(!(expected == actual)) {
failNotEquals(new Double(actual), new Double(expected), message);
}
}
else if(!(Math.abs(expected - actua