python+selenium之斷言Assertion

1、斷言方法

斷言是對自動化測試異常狀況的判斷。python

# -*- 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方法,採用徹底相等方法,建議選擇第一種方法web

這u表明unicode的意思,因爲咱們這裏採用了python 2, 若是你使用pyn3 就不須要,在Python3中,字符串默認採用unicode存儲。框架

2、斷言方法

在執行用例的過程當中,最終用例是否經過,是經過判斷測試的到的實際結果與預測結果是否相等絕頂的。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爲可選參數,用於定義測試失敗時打印的信息。ui

 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 ()

 3、assertTrue與assertFalse

用於測試表達式是true仍是false。spa

下面來實現判斷一個數是否未數的功能,所謂的質數(又叫素數)是指只能被1和她自己整除的數。code

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()斷言獲得的結果進行判斷。orm

4、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的一個實例。

相關文章
相關標籤/搜索