python3的unittest中使用test suite(测试套件)执行指定测试用例

示例代码

module.py

class baidumodule():
def __init__(self,driver,):
self.dr = driver #不能在类中再次导入webdriver 两边的driver等于两个窗口,直接让调用方传入driver即可
def login(self,values):
login_dr = self.dr
login_dr.get("https://www.baidu.com/")
login_dr.find_element_by_xpath("//*[@id='kw']").send_keys(values)
login_dr.find_element_by_xpath("//*[@id='su']").click() def login_out(self):
self.dr.quit()

baidu.py

# _*_ coding:utf-8 _*_
import csv,unittest #导入csv模块
from time import sleep
from selenium import webdriver
from module import baidumodule
class baidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()#最大化窗口
self.driver.implicitly_wait(10)#隐式等待
self.search = baidumodule(self.driver) #将driver传给aidumodule这个类
with open("file.csv","r") as name:
self.lines = name.readlines()#以行读取整个文件
def tearDown(self):
self.search.login_out() def test_search(self):
search = self.search
lines = self.lines
driver = self.driver
search.login(lines[0])
sleep(1)
title = driver.title
self.assertEqual(title,'selenium_百度搜索2')
sleep(2)
def test_search1(self):
search = self.search
lines = self.lines
driver = self.driver
search.login(lines[1])
sleep(1)
title = driver.title
self.assertEqual(title,'selenium2_百度搜索')
sleep(2)
def test_search2(self):
search = self.search
lines = self.lines
driver = self.driver
search.login(lines[2])
sleep(1)
title = driver.title
self.assertEqual(title,'selenium3_百度搜索')
sleep(2)
def test_search3(self):
search = self.search
lines = self.lines
driver = self.driver
search.login(lines[3])
sleep(1)
title = driver.title
self.assertEqual(title,'webdriver_百度搜索')
sleep(2) class counttest(unittest.TestCase):
def setUp(self):
self.b = 2
self.c = 3
def test_add(self):
d = self.b + self.c
self.assertEqual(d,5) if __name__ == "__main__":
unittest.main()

如上我们有可能拥有n多条测试用例,我们现新增了一条test_add,我们只需要单独执行这条即可稍微改动一下if __name__ == "__main__":的代码即可

# _*_ coding:utf-8 _*_
import csv,unittest #导入csv模块
from time import sleep
from selenium import webdriver
from module import baidumodule
class baidu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()#最大化窗口
self.driver.implicitly_wait(10)#隐式等待
self.search = baidumodule(self.driver) #将driver传给aidumodule这个类
with open("file.csv","r") as name:
self.lines = name.readlines()#以行读取整个文件
def tearDown(self):
self.search.login_out() def test_search(self):
search = self.search
lines = self.lines
driver = self.driver
search.login(lines[0])
sleep(1)
title = driver.title
self.assertEqual(title,'selenium_百度搜索2')
sleep(2)
def test_search1(self):
search = self.search
lines = self.lines
driver = self.driver
search.login(lines[1])
sleep(1)
title = driver.title
self.assertEqual(title,'selenium2_百度搜索')
sleep(2)
def test_search2(self):
search = self.search
lines = self.lines
driver = self.driver
search.login(lines[2])
sleep(1)
title = driver.title
self.assertEqual(title,'selenium3_百度搜索')
sleep(2)
def test_search3(self):
search = self.search
lines = self.lines
driver = self.driver
search.login(lines[3])
sleep(1)
title = driver.title
self.assertEqual(title,'webdriver_百度搜索')
sleep(2) class counttest(unittest.TestCase):
def setUp(self):
self.b = 2
self.c = 3
def test_add(self):
d = self.b + self.c
self.assertEqual(d,5) if __name__ == "__main__": #如果直接执行将执行以下代码,调用不执行以下代码
# unittest.main()
#构造测试集
suit = unittest.TestSuite()
suit.addTest(counttest("test_add"))#把这个类中需要执行的测试用例加进去,有多条再加即可
#suit.addTest(counttest("test_add2"))#从上到下先后顺序
runner = unittest.TextTestRunner()
runner.run(suit)#运行测试用例

这样就只会执行我们指定的用例了

上一篇:Paper intensive reading (二十三): Mitophagy inhibits amyloid-β and tau pathology ... in Alzheimer


下一篇:局部加权之逻辑回归(1) - Python实现