python+selenium之断言Assertion

一、断言方法

断言是对自动化测试异常情况的判断。

# -*- coding: utf-8 -*-
from selenium import webdriver
import unittest
import os,sys,time
import HTMLTestReport

#登录
driver =webdriver.Firefox()

current_time = time.strftime("%Y-%m-%d-%H_%M_%S", time.localtime(time.time()))
current_time1 = time.strftime("%Y-%m-%d", time.localtime(time.time()))
print(current_time )
print(current_time1 )
# 必须打印图片路径HTMLTestRunner才能捕获并且生成路径,image**\**.png 是获取路径的条件,必须这样的目录
#设置存储图片路径,测试结果图片可以按照每天进行区分

pic_path = '.\result\image\' + current_time1+'\' + current_time +'.png'
print(pic_path)
time.sleep(5)

#通过if进行断言判断
driver.get("https://baidu.com/")
print(driver.title)
driver.save_screenshot(pic_path)
if u'百度一下,你就知道' == driver.title:
    print ('Assertion test pass.') 
else:
    print ('Assertion test fail.')

 #通过try抛出异常进行断言判断   
driver.get("https://baidu.com/")
driver.save_screenshot(pic_path)
try:
    assert  u'百度一下,你就知道' ==  driver.title
    print ('Assertion test pass.')  
except Exception as e:
    print ('Assertion test fail.', format(e))

time.sleep(5)
driver.quit()

方法一,是利用python中Assert方法,采用包含判断,方法二是通过if方法,采用完全相等方法,建议选择第一种方法

这u代表unicode的意思,由于我们这里采用了python 2, 如果你使用pyn3 就不需要,在Python3中,字符串默认采用unicode存储。

二、断言方法

在执行用例的过程中,最终用例是否通过,是通过判断测试的到的实际结果与预测结果是否相等绝顶的。unittest框架的TestCase类提供下面这些方法用于测试结果的判断。

 1 from calculator import Count
 2 import unittest
 3 
 4 
 5 class TestCount (unittest.TestCase):
 6     def setUp(self):
 7         print("test start")
 8 
 9     def test_add(self):
10         j = Count (2, 4)
11         self.assertEqual (j.add (), 6)
12 
13     def test_add2(self):
14         i = Count (2,9)
15         self.assertEqual (i.add (), 11)
16 
17     def tearDown(self):
18         print("test end")
19 
20 
21 if __name__ == '__main__':
22     # unittest.main ()
23     suite = unittest.TestSuite()
24     suite.addTest(TestCount('test_add2'))
25     suite.addTest (TestCount ('test_add'))
26     runner = unittest.TextTestRunner()
27     runner.run(suite)
方法 检查 版本
assertEqual(a,b) a==b  
assertNotEqual(a,b) a!=b  
asserTrue(x) bool(x) is true  
asserFalse(x) bool(x) is False  
assertIs(a,b) a is b  
assertIsNot(a,b) a is not b  
assertIsNone(x) x is None  
assertIsNoneNot(x) x is not None  
assertIn(a,b) a in b  
assertInNot a not in b  
assertIsInstance(a,b) isinstance(a,b)  
assertNotIsInstance(a,b) not isinstance (a,b)  

***assertEqual(first ,second ,msg =None)

断言第一个参数和第二个参数是否相等,如果 不相等则测试失败。msg为可选参数,用于定义测试失败时打印的信息。

 1 import unittest
 2 
 3 
 4 class assertEqual1(unittest.TestCase):
 5     def setUp(self):
 6         number = input ("Enter a number:")
 7         self.number = int (number)
 8 
 9     def test_case(self):
10         self.assertEqual (self.number, 10, msg='Your input is not 10!')
11 
12     def tearDown(self):
13         pass
14 
15 
16 if __name__ == '__main__':
17     unittest.main ()

 三、assertTrue与assertFalse

用于测试表达式是true还是false。

下面来实现判断一个数是否未数的功能,所谓的质数(又叫素数)是指只能被1和她本身整除的数。

1 def is_prime(n):
2     if n <= 1:
3         return False
4     for i in range (2, n):
5         if n % i == 0:
6             return False
7         else:
8             return True
 1 from calculator import is_prime
 2 import unittest
 3 
 4 
 5 class test (unittest.TestCase):
 6     def setUp(self):
 7         print("测试开始")
 8 
 9     def test_case(self):
10         self.assertTrue (is_prime(3), msg="Is not prime!")
11 
12     def tearDown(self):
13         print("测试结束")
14 
15 
16 if __name__ == '__main__':
17     unittest.main ()

 在调用is_prime()函数时分别传不同的值来执行测试用例,然后通过assertTrue()断言得到的结果进行判断。

四、assertIn(first,second,msg = None)与assertNotIn(first,second,msg = None)

注:断言第一个参数是否在第二个参数中,第二个表示参数是否包含第一个参数。

 1 import unittest
 2 
 3 
 4 class test (unittest.TestCase):
 5     def setUp(self):
 6         print("测试开始")
 7 
 8     def test_case(self):
 9         a = 'hello'
10         b = 'hello word!'
11         # self.assertIn (b, a, msg='a is not in b')
12         self.assertNotIn (a, b, msg='a is not in b')
13 
14     def tearDown(self):
15         print("测试结束")
16 
17 
18 if __name__ == '__main__':
19     unittest.main ()
  1. assertIs(first,second,msg=None) 与assertIsNot(first,second,msg=None)

             断言第一个参数和第二个参数是否为同一个对象。

        2.assertIsNone(expr,msg = None)与assertIsNone(expr,msg = None)

            断言表达式是否为None对象。

        3.assertIsInstance(obj,cls,msg = None)与assertIsInstance(obj,cls,msg = None)

            断言obj是否为cls的一个实例。

原文地址:https://www.cnblogs.com/fengyiru6369/p/7234562.html