Python版本:3.6.4html
selenium版本:3.11.0python
>>> import selenium >>> help(selenium)
IDE:Pycharmlinux
運行工做圖:windows
運行數據流:框架
簡單示例:less
import unittest class TestStringMethods(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('FFF'.isupper(),msg='wrong flag') def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) if __name__ == '__main__': unittest.main()
運行結果:學習
F.. ====================================================================== FAIL: test_isupper (__main__.TestStringMethods) ---------------------------------------------------------------------- Traceback (most recent call last): File "F:/python_stack/python_autotest/demo.py", line 10, in test_isupper self.assertFalse('FFF'.isupper(),msg='wrong flag') AssertionError: True is not false : wrong flag ---------------------------------------------------------------------- Ran 3 tests in 0.001s FAILED (failures=1)
運行結果告訴咱們:測試
1.測試用例執行後,結果順序是隨機排序;ui
2.測試用例以test爲前綴;編碼
3.若是想單獨運行一個用例,點擊相應的測試用例代碼區域,右鍵點擊運行相應的方法
4,運行測試套件能夠點擊run(alt+shift+F10)
Step2:test fixture之setUp() + tearDown() 和 setUpClass() 與 tearDownClass()
setUp() + tearDown() :在每一個測試方法執行前以及執行後執行一次,setUp用來爲測試準備環境,tearDown用來清理環境,準備以後的測試
setUpClass() 與 tearDownClass():在全部case執行以前準備一次環境,並在全部case執行結束以後再清理環境
實例代碼:
import unittest class TestStringMethods(unittest.TestCase): @classmethod def setUpClass(cls): #只執行一次,在全部用例開始前執行,通常用來預製數據,也能夠爲下發自動化task初始化 print('setUpClass'+'\n') @classmethod def tearDownClass(cls): #只執行一次,在所用測試用例執行完畢後運行,通常用來清理測試環境 print('tearDownClass'+'\n') def setUp(self): # 每一個用例都執行,在單個用例運行前執行 print('準備開始執行用例'+'\n') def tearDown(self): #每一個用例都執行,在單個用例運行後執行 print('清理此用例的初始化'+'\n') def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') print('test_upper'+'\n') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper(),msg='wrong flag') print('test_isupper'+'\n') def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) print('test_split'+'\n') if __name__ == '__main__': unittest.main()
運行結果:
... ---------------------------------------------------------------------- Ran 3 tests in 0.000s OK setUpClass 準備開始執行用例 test_isupper 清理此用例的初始化 準備開始執行用例 test_split 清理此用例的初始化 準備開始執行用例 test_upper 清理此用例的初始化 tearDownClass
test suite(測試套件)的做用是批量運行多個測試用例,此外還能夠作的操做是:
import unittest class TestStringMethods(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) class MathMethods(unittest.TestCase): def test_sum(self): s = 'Python' self.assertNotEquals('python',s.islower()) if __name__ == '__main__': testcase1 = unittest.TestLoader().loadTestsFromTestCase(MathMethods) testcase2 = unittest.TestLoader().loadTestsFromTestCase(TestStringMethods) suite = unittest.TestSuite([testcase1,testcase2]) #verbosity的參數爲0/1/2,2的回顯結果最詳細 unittest.TextTestRunner(verbosity=2).run(suite)
運行結果:
test_sum (__main__.MathMethods) ... ok test_isupper (__main__.TestStringMethods) ... ok test_split (__main__.TestStringMethods) ... ok test_upper (__main__.TestStringMethods) ... ok ---------------------------------------------------------------------- Ran 4 tests in 0.001s OK
import unittest class TestStringMethods(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') print('test_upper') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) print('test_isupper') def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) print('test_split') if __name__ == '__main__': print('單個單個添加測試用例') suite = unittest.TestSuite() suite.addTest(TestStringMethods('test_upper')) suite.addTest(TestStringMethods('test_split')) suite.addTest(TestStringMethods('test_isupper')) runner = unittest.TextTestRunner() runner.run(suite) print('同時添加多個測試用例') suite1 = unittest.TestSuite() suite1.addTests([TestStringMethods('test_split'),TestStringMethods('test_isupper'),TestStringMethods('test_upper')]) runner2 = unittest.TextTestRunner() runner2.run(suite1)
import unittest import sys class TestStringMethods(unittest.TestCase): @unittest.skipIf('F'=='f','不知足判斷條件就執行') def test_upper2(self): self.assertEqual('foo'.upper(), 'FOO') print('test_upper2') @unittest.skipIf('F'=='f'.upper(),'知足判斷條件就不執行') def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') print('test_upper') @unittest.skip('忽略此用例不執行') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) print('test_isupper') #skipUnless表示若是系統名稱是linux,用例就忽略執行,提示用戶使用win,sys.platform返回操做系統平臺名稱 #Python startswith() 方法用於檢查字符串是不是以指定子字符串開頭 @unittest.skipUnless(sys.platform.startswith('linux'),'we need windows') def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) print('test_split') if __name__ == '__main__': suite1 = unittest.TestSuite() suite1.addTests([TestStringMethods('test_upper2'),TestStringMethods('test_split'),TestStringMethods('test_isupper'),TestStringMethods('test_upper')]) runner2 = unittest.TextTestRunner(verbosity=2) runner2.run(suite1)
運行結果:
test_upper2 (__main__.TestStringMethods) ... ok test_upper2 test_split (__main__.TestStringMethods) ... skipped 'we need windows' test_isupper (__main__.TestStringMethods) ... skipped '忽略此用例不執行' test_upper (__main__.TestStringMethods) ... skipped '知足判斷條件就不執行' ---------------------------------------------------------------------- Ran 4 tests in 0.001s OK (skipped=3)
import unittest import sys class TestStringMethods(unittest.TestCase): @unittest.skipIf('F'=='f','不知足判斷條件就執行') def test_upper2(self): self.assertEqual('foo'.upper(), 'FOO') print('test_upper2') @unittest.skipIf('F'=='f'.upper(),'知足判斷條件就不執行') def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') print('test_upper') @unittest.skip('忽略此用例不執行') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) print('test_isupper') #skipUnless表示若是系統名稱是linux,用例就忽略執行,提示用戶使用win,sys.platform返回操做系統平臺名稱 #Python startswith() 方法用於檢查字符串是不是以指定子字符串開頭 @unittest.skipUnless(sys.platform.startswith('linux'),'we need windows') def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) print('test_split') if __name__ == '__main__': suite1 = unittest.TestSuite() suite1.addTests([TestStringMethods('test_upper2'),TestStringMethods('test_split'),TestStringMethods('test_isupper'),TestStringMethods('test_upper')]) with open('result.txt','a+',encoding='utf-8') as f: runner2 = unittest.TextTestRunner(stream=f,verbosity=2) runner2.run(suite1)
方法就是上述代碼所示
測試腳本:
import unittest import os from HTMLTestRunner import HTMLTestRunner class TestStringMethods(unittest.TestCase): def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') print('test_upper') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) print('test_isupper') def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): s.split(2) print('test_split') if __name__ == '__main__': report = os.path.join('D:/Python36/report/report.html') suite1 = unittest.TestSuite() suite1.addTests([TestStringMethods('test_split'),TestStringMethods('test_isupper'),TestStringMethods('test_upper')]) with open(report,'wb') as f: runner2 = HTMLTestRunner(stream=f,title='Test Result',description='operator:admin',verbosity=2) runner2.run(suite1)
測試結果:
HTMLTestRunner腳原本自:https://blog.csdn.net/huilan_same/article/details/77944829
1. Python3不少測試類不支持,沒有Python2那麼好找解決辦法
2. 效率太慢,明天繼續
磨磨蹭蹭終於開始作本身想作的事情了,但願半個月到一個月內,能夠輸出stepbystep的測試步驟,並且是Python3腳本,挺有意思的,就是公司沒有外網,坑啊
https://docs.python.org/3.6/library/unittest.html#
https://blog.csdn.net/huilan_same/article/details/52944782