python单元测试框架
unittest官方文档 https://docs.python.org/2.7/library/unittest.html unittest不仅适用于单元测试,还可以适用于Web自动化测试用例的开发和执行
unittest核心要素
TestCase TestCase实例是一个测试用例,就是一个完整的测试流程,包括测试前的准备环境的搭建(setUp),执行测试代码(run),以及测试后环境的还原(tearTown)。 一个测试用例就是一个完整的测试单元TestSuite 多个测试用例集合在一起就是TestSuite,可以将测试用例添加到TestSuit集合中TextTestRunner 执行测试用例
对Math类 进行单元测试
Math.py
class Math:
def __init__(self,a,b):
self.a=int(a)
self.b=int(b)
def add(self):
return self.a+self.b
def sub(self):
return self.a-self.b
搭建测试环境 unittest_startend.py
import unittest
class Test_StartEnd(unittest.TestCase):
def setUp(self):
print("start up")
def tearDown(self):
print("end")
add方法测试实例 unittest_add.py
class Test_add(Test_StartEnd):
def test_add(self):
j=Math(5,5)
self.assertEqual(j.add(),10)
def test_add1(self):
j=Math(-45.8,-12.9)
self.assertEqual(j.add(), -57)
def test_add2(self):
j = Math(45.1,12.3)
self.assertEqual(j.add(), 57)
def test_add3(self):
j=Math(64376,37463)
self.assertEqual(j.add(), 101839)
def test_add4(self):
j= Math('1','34')
self.assertEqual(j.add(), 35)
sub方法测试实例
unittest_sub.py
class Test_sub(Test_StartEnd):
def test_sub(self):
j = Math(23,3)
self.assertEqual(j.sub(),20 )
def test_sub1(self):
j = Math(45.1,5.3)
self.assertEqual(j.sub(), 40)
def test_sub2(self):
j=Math(-5,-6)
self.assertEqual(j.sub(), 1)
def test_sub3(self):
j= Math('34','-9')
self.assertEqual(j.sub(), 43)
runtest.py执行测试用例 1.unittest.main()方法 2.添加测试集合方法
#构造测试集
suite=unittest.TestSuite()
suite.addTest(Test_sub("test_sub"))
#执行测试
runner=unittest.TextTestRunner()
runner.run(suite)
3.discover方法,可以一次调用多个脚本
test_dir=r'./'
discover=unittest.defaultTestLoader.discover(test_dir, pattern="test_*.py")
if __name__=='__main__':
runner=unittest.TextTestRunner()
runner.run(discover)
补充,测试用例的跳转 unittest.skip() 直接跳过测试 unittest.skipIf() 条件为真,跳过测试 unittest.skipUnless 条件为假,跳过测试 unittest.expectedFailure 预期设置失败
class Test1(unittest.TestCase):
def setUp(self):
print("Test1 start")
@unittest.skipIf(4>3, "skip test_b")
def test_c(self):
print("test_c")
@unittest.skipIf(1<0, "skip test_c")
def test_b(self):
print("test_b")
@unittest.expectedFailure
def test_a(self):
self.assertEqual(1+1,0)
def tearDown(self):
print("Test1 end")
web测试用例
qunar自动登录测试脚本test_qunar.py
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
class qunar_Login():
def __init__(self,driver,username,password):
self.driver=driver
self.username=username
self.password=password
def login(self):
element=WebDriverWait(self.driver,5).until(expected_conditions.presence_of_element_located(By.ID,'username'))
element.send_keys(self.username)
element=WebDriverWait(self.driver,5).until(expected_conditions.presence_of_element_located(By.ID,'password'))
element.send_keys(self.password)
element=WebDriverWait(self.driver,5).until(expected_conditions.presence_of_element_located(By.ID,'su'))
element.click()
def quit(self):
self.driver.find_element_by_css_selector('.退出').click()
self.driver.switch_to_alert('accept')
def main():
driver=webdriver.Firefox()
driver.get('qunar')
driver.maximize_window()
driver.implicitly_wait(5)
qunar_Login(driver,'user1','123').login()
driver.get_screenshot_as_file(r'e:/')
qunar_Login(driver,'user2','123').quit()
driver.get_screenshot_as_file(r'e:/')
qunar自动登录单元测试unittest_qunar.py
import unittest
from selenium import webdriver
from qunar.test_qunar import qunar_Login
class Test_qunar_login(unittest.TestCase):
#构建测试环境
def setUp(self):
self.driver=webdriver.Firefox()
self.driver.get('qunar')
self.driver.implicitly_wait(5)
def test_login_case1(self):
qunar_Login(self.driver,'user1','123').login()
def test_login_case2(self):
qunar_Login(self.driver,'user2','123').login()
#退出测试环境
def tearDown(self):
self.driver.quit()
if __name__ =='__main__':
unittest.main()
测试报告
#-*- coding:utf-8 -*-
from HTMLTestRunner import HTMLTestRunner
import unittest
import time
test_dir = r'../test_project'
discover=unittest.defaultTestLoader.discover(test_dir,pattern="test_*.py")
if __name__ == '__main__':
report_dir = './'
now=time.strftime("%Y-%m-%d %H_%M_%S")
report_name=report_dir+now+'result.html'
print(report_name)
with open(report_name,'wb') as f:
runner=HTMLTestRunner(stream=f,title="Test Report",description="Test case result")
runner.run(discover)
f.close()
#-*- coding:utf-8 -*-
from BSTestRunner import BSTestRunner
import unittest
import time
test_dir = r'../test_project'
discover=unittest.defaultTestLoader.discover(test_dir,pattern="test_*.py")
if __name__ == '__main__':
report_dir = './'
now=time.strftime("%Y-%m-%d %H_%M_%S")
report_name=report_dir+now+'result.html'
with open(report_name,'wb')as f:
runer=BSTestRunner(stream=f,title="Test Report",description='Test case result')
runer.run(discover)
f.close()