Python-Unittest

TestCase——> Test Fixure测试固件

|

TestSuite测试套件——>TestRunner测试运行器

|

TestReport

测试断言

verbosity=2

0代表得到执行的测试总数和全局结果

1代表得到成功的显示,失败的显示f,错误的显示e,默认的是1

2可以得到详细的信息

1.unittest各个组件

2.unittest测试固件

a.setUp&tearDown

b.setUpClass&tearDownClass

#!/usr/bin/env python
#coding:utf-9 import unittest
import selenium import webdriver class F2(unittest.TestCase):
@classmethod
def selfUpClass(cls):
cls.driver=webdriver.Chrome()
cls.driver.maximize_window()
cls.driver.implicityly_wait(30)
cls.driver.get("http://www.baidu.com")
@classmethod
def tearDown(cls):
cls.driver.quit()
def test_baidu_new(self):
self.driver.find_element_by_link_text('新闻').click()
def test_baidu_map(self):
self.driver.find_element_by_link_text('地图').click()
if __name__=="__main__":
unittest.main(verbosity=2)
类方法:会出现找不到元素的问题
改写成:
def test_baidu_new(self):
self.driver.find_element_by_link_text('新闻').click()
self.driver.back() import  unittest
from selenium import  webdriver class F3(unittest.TestCase):
    def setUp(self):
        self.driver=webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(30)
        self.driver.get('http://www.baidu.com')     def tearDown(self):
        self.driver.quit()     def test_baidu_news(self):
        self.driver.find_element_by_link_text('新闻').click()     def test_baidu_map(self):
        self.driver.find_element_by_partial_link_text('图').click() if __name__ == '__main__':
    unittest.main(verbosity=2)

c.程序执行的顺序

d.测试用例的编写方法

写注释,好判断是哪个用例有错误

3.测试执行

4.构建测试套件,就是测试集

a用例按顺序执行(addTest)

#!/usr/bin/env python
#coding:utf-9 import unittest
import selenium import webdriver class BaiduTest(unittest.TestCase):
@classmethod
def selfUpClass(cls):
cls.driver=webdriver.Chrome()
cls.driver.maximize_window()
cls.driver.implicityly_wait(30)
cls.driver.get("http://www.baidu.com")
@classmethod
def tearDown(cls):
cls.driver.quit()
def test_baidu_new(self):
self.driver.find_element_by_link_text('新闻').click()
self.driver.back()
def test_baidu_map(self):
self.driver.find_element_by_link_text('地图').click()
self.driver.back()
if __name__=="__main__":
suite=unittest.TestSuite()初始化
suite.addTest(BaiduTest('test_baidu_new'))
suite.addTest(BaiduTest('test_baidu_man'))
unittest.TextTestRunner(verbosity=2).run(suite)
执行顺序还是会按照ASCII字母顺序执行,
解决方法就是把用例名用数字编号显示,但是addTest会产生很多相同代码,解决办法就是按类执行
if __name__=="__main__":
suite=unittest.TestSuite()初始化
suite.addTest(BaiduTest)
unittest.TextTestRunner(verbosity=2).run(suite)

b.测试用例执行顺序的详解

c.按测试类执行makeSuite,是个函数

#!/usr/bin/env python
# coding:utf-8 import unittest
from selenium import webdriver class F7(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def test_001(self):
pass
def test_002(self):
pass
if __name__=="__main__":
suite=unittest.TestSuite(unittest.makeSuite(F7))
unittest.TextTestRunner(verbosity=2).run(suite)

d.加载测试类

e按测试模块来执行

if __name__=="__main__":
suite=unittest.TestLoader().loadTestsFromModult('f8.py')
unittest.TextTestRunner(verbosity=2).run(suite)

f优化测试套件

#!/usr/bin/env python
# coding:utf-8 import unittest
from selenium import webdriver class BaiduLink(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicitly_wait(30)
self.driver.get('http://www.baidu.com')
def tearDown(self):
self.driver.quit()
pass
def test_001(self):
self.driver.find_element_by_link_text('新闻').click()
def test_002(self):
self.driver.find_element_by_link_text('地图').click()
@staticmethod
def suite():
suite = unittest.TestSuite(unittest.makeSuite(BaiduLink))
return suite
if __name__ =="__main__":
unittest.TextTestRunner(verbosity=2).run(BaiduLink.suite())

5.分离测试固件(模块化的应用)

init.py

#! /usr/bin/env python
codring:utf-8 import unittest
from selenium import webdriver
class Init(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.driver.implicity_wait(30)
self.driver.get('http://www.baidu.com')
def tearDown(self):
self.driver.quit()
#!/usr/bin/env python
# coding:utf-8
import unittest from Day10.Init import *
class BaiduSo(Init): self test_baidu_news(self):
self.driver.find_element_by_id('kw').send_keys('webdriver') if __name__=="__main__":
unittest.main(verbosity=2) 取消测试用例
@unittest.skip('该功能已经取消,忽略该测试用例的执行')
@unittest.expectedFailure 期望测试用例执行失败
 

6.测试断言

assertEqual(a,b)  内容一致,类型一致

#coding:utf-8
import unittest
from Day10.init import * class BaiduLink(Init):
def test_baidu_news(self):
print(self.driver.title,type(self.driver.title))
self.assertEqual(self.driver.title,u'百度一下,你就知道') if __name__=="__main__":
unittest.main(verbosity=2)

assertTrue(x)

def test_baidu_so(self):
so = self.driver.find_element_by_id('kw')
print so.is_enable()
self.assertTrue(so.is_enabled())

assertIn(a,b)

def test_baidu_title(self):
self.assertIn('百度',self.driver.title)

测试断言注意事项

   不正确的使用if

  不正确的使用异常

7.批量执行测试用例(discover)

#!/usr/bin/env python
# coding:utf-8 import unittest
import os def allTests():
suite=unittest.TestLoader.discover(
start_dir = os.path.dirname(__file__),
pattern = 'test_*.py',
top_level_dir=None)
return suite def run():
unittest.TextTestRunner(verbosity=2).run(allTests()) if __name__=="__main__":
run()

a.获取所有测试用例

b.discover方法的解读

8.生成测试报告

a.python2测试报告的生成

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

b.python3测试报告的生成

!/usr/bin/env python
# coding:utf-8 import unittest
import os
import HTMLTestRunner def allTests():
suite=unittest.TestLoader.discover(
start_dir = os.path.dirname(__file__),
pattern = 'test_*.py',
top_level_dir=None)
return suite def run():
fp=os.path.join(os.path.dirname(__file__),'report','testReport.html')
HTMLTestRunner.HTMLRunner(
stream = open(fp,'wb'),
title ='自动化测试报告',
description='自动化测试详细信息').run(allTests()) if __name__=="__main__":
run()

c.测试报告的注意事项

d.新增当前时间

9.unittest的缺陷

10.Coverage的应用

上一篇:Noip 2016


下一篇:Activity完整的生命周期