本文主要是介绍【功能自动化】使用测试套件运行测试函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.创建registers.py
将registers.py放在文件夹下
registers.py
代码实现
# 导入包
from selenium import webdriver
from selenium.webdriver.support.select import Select
from time import sleep
import unittest
import parameterizeddriver = None
file = open(r"user.txt")
data = []
for row in file:t = row.split()data.append(t)class Reg(unittest.TestCase):@classmethoddef setUpClass(cls):global driverdriver = webdriver.Chrome()@classmethoddef tearDownClass(cls):driver.quit()# def setUp(self):# driver.get("http://127.0.0.1:1080/WebTours/")# sleep(3)def testOpenWeb(self):u'''打开网站'''driver.get("http://127.0.0.1:1080/WebTours/")# 等待3秒sleep(3)driver.switch_to.default_content()driver.switch_to_frame("body")driver.switch_to_frame("info")actual = driver.find_element_by_tag_name("body").textexpect = 'Welcome to the Web Tours site.'assert expect in actual,"实际结果:"+actual@parameterized.parameterized.expand(data)def testRegister(self,yhm,mm):u'''注册账号'''driver.get("http://127.0.0.1:1080/WebTours/")sleep(1)driver.switch_to.default_content()driver.switch_to.frame("body")driver.switch_to.frame("info")driver.find_element_by_link_text("sign up now").click()sleep(3)# 跳转页面,还是需要切换框架driver.switch_to.default_content()driver.switch_to.frame("body")driver.switch_to.frame("info")driver.find_element_by_name("username").send_keys(yhm)driver.find_element_by_name("password").send_keys(mm)driver.find_element_by_name("passwordConfirm").send_keys(mm)driver.find_element_by_name("register").click()sleep(3)driver.switch_to.default_content()driver.switch_to.frame("body")driver.switch_to.frame("info")expect = "Thank you, "+yhmactual = driver.find_element_by_tag_name("body").textassert expect in actual,"实际结果:"+actualif __name__=="__main__":# verbosity=2 显示详细信息,可以省略,测试信息不详细unittest.main(verbosity=2)
2.使用测试套件运行测试函数
代码实现
import unittest
from test.registers import Reg
suite = unittest.TestSuite()
suite.addTest(Reg("testOpenWeb"))
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
3.运行结果
这篇关于【功能自动化】使用测试套件运行测试函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!