python测试开发测试开发AutoTest-AutoAI

python_常用断言assert

2016-10-11  本文已影响7474人  古佛青灯度流年

python自动化测试中寻找元素并进行操作,如果在元素好找的情况下,相信大家都可以较熟练地编写用例脚本了,但光进行操作可能还不够,有时候也需要对预期结果进行判断。

assertEqual(a, b)     a == b      
assertNotEqual(a, b)     a != b      
assertTrue(x)     bool(x) is True      
assertFalse(x)     bool(x) is False      
assertIs(a, b)     a is b     2.7
assertIsNot(a, b)     a is not b     2.7
assertIsNone(x)     x is None     2.7
assertIsNotNone(x)     x is not None     2.7
assertIn(a, b)     a in b     2.7
assertNotIn(a, b)     a not in b     2.7
assertIsInstance(a, b)     isinstance(a, b)     2.7
assertNotIsInstance(a, b)     not isinstance(a, b)     2.7
assertAlmostEqual(a, b)     round(a-b, 7) == 0      
assertNotAlmostEqual(a, b)     round(a-b, 7) != 0      
assertGreater(a, b)     a > b     2.7
assertGreaterEqual(a, b)     a >= b     2.7
assertLess(a, b)     a < b     2.7
assertLessEqual(a, b)     a <= b     2.7
assertRegexpMatches(s, re)     regex.search(s)     2.7
assertNotRegexpMatches(s, re)     not regex.search(s)     2.7
assertItemsEqual(a, b)     sorted(a) == sorted(b) and works with unhashable objs     2.7
assertDictContainsSubset(a, b)     all the key/value pairs in a exist in b     2.7
assertMultiLineEqual(a, b)     strings     2.7
assertSequenceEqual(a, b)     sequences     2.7
assertListEqual(a, b)     lists     2.7
assertTupleEqual(a, b)     tuples     2.7
assertSetEqual(a, b)     sets or frozensets     2.7
assertDictEqual(a, b)     dicts     2.7
assertMultiLineEqual(a, b)     strings     2.7
assertSequenceEqual(a, b)     sequences     2.7
assertListEqual(a, b)     lists     2.7
assertTupleEqual(a, b)     tuples     2.7
assertSetEqual(a, b)     sets or frozensets     2.7assertDictEqual(a, b)     dicts     2.7

那么我们什么时候应该使用断言呢?如果没有特别的目的,断言应该用于如下情况:

assert target in(x, y, z)
if  target ==x:  
    run_x_code()
elif  target ==y:
    run_y_code()
else:
    assert target == z
    run_z_code()
上一篇 下一篇

猜你喜欢

热点阅读