当前位置:网站首页>Unittest framework
Unittest framework
2022-04-22 08:08:00 【Short man code】
import unittest
import time
from selenium import webdriver
import os
class test_baidu1(unittest.TestCase):
def setUp(self):
# Get drive path
current_path = os.path.dirname(__file__) # Get the current path
driver_path = os.path.join(current_path, '../../../driver/chromedriver.exe') # Put the current path +chromedriver Path connection
self.driver = webdriver.Chrome(executable_path=driver_path)
self.driver.get('https://www.baidu.com')
self.driver.maximize_window()
def tearDown(self):
time.sleep(1)
self.driver.quit()
def test_cat(self):
self.driver.find_element_by_name('wd').send_keys(' Panda ')
self.driver.find_element_by_id('su').click()
time.sleep(1)
self.assertEqual(self.driver.title, " Panda _ Baidu search ")
def test_dog(self):
self.driver.find_element_by_name('wd').send_keys(' Dog ')
self.driver.find_element_by_id('su').click()
time.sleep(1)
self.assertEqual(self.driver.title, " Dog _ Baidu search ")
def test_pig(self):
self.driver.find_element_by_name('wd').send_keys(' The pig ')
self.driver.find_element_by_id('su').click()
time.sleep(1)
self.assertEqual(self.driver.title, " The pig _ Baidu search ")
def suite():
suite = unittest.TestSuite() # Create a suite object
suite.addTest(test_baidu1('test_cat')) # Class name ( Method name )
suite.addTest(test_baidu1('test_dog')) # Class name ( Method name )
suite.addTest(test_baidu1('test_pig')) # Class name ( Method name )
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite') # Only the methods in the suite will be executed
import unittest
import time
from selenium import webdriver
import os
class test_baidu2(unittest.TestCase):
def setUp(self):
# Get drive path
current_path = os.path.dirname(__file__) # Get the current path
driver_path = os.path.join(current_path, '../../../driver/chromedriver.exe') # Put the current path +chromedriver Path connection
self.driver = webdriver.Chrome(executable_path=driver_path)
self.driver.get('https://www.baidu.com')
self.driver.maximize_window()
def tearDown(self):
time.sleep(1)
self.driver.quit()
def test_cat(self):
self.driver.find_element_by_name('wd').send_keys(' Worm ')
self.driver.find_element_by_id('su').click()
time.sleep(1)
self.assertEqual(self.driver.title, " Worm _ Baidu search ")
def test_dog(self):
self.driver.find_element_by_name('wd').send_keys(' bear ')
self.driver.find_element_by_id('su').click()
time.sleep(1)
self.assertEqual(self.driver.title, " bear _ Baidu search ")
def test_pig(self):
self.driver.find_element_by_name('wd').send_keys(' Tujia pig ')
self.driver.find_element_by_id('su').click()
time.sleep(1)
self.assertEqual(self.driver.title, " Tujia pig _ Baidu search ")
def suite():
suite = unittest.TestSuite() # Create a suite object
suite.addTest(test_baidu2('test_cat')) # Class name ( Method name )
suite.addTest(test_baidu2('test_dog')) # Class name ( Method name )
suite.addTest(test_baidu2('test_pig')) # Class name ( Method name )
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite') # Only the methods in the suite will be executed
# Operation main entrance , perform test_case Remove all modules
import os
# Get all the file names in the directory
current_path = os.path.dirname(__file__) # Get local path
case_path = os.path.join(current_path, 'unittest_case') # Local path into unittest_case file
file_name = os.listdir(case_path) # lookup unitest_case Next .py The file of ['dome01.py', 'dome02.py', '__init__.py']
print(file_name)
# To perform all .py Final document
for file in file_name: # Traverse file_name file
f = file.split('.')[-1] # cutting ['dome01', 'py'] ['dome02', 'py'] ['__init__', 'py']
if f == 'py':
os.system('python ' + case_path+'/'+file+' 1>>log.txt 2>&1')
import os, unittest, datetime
import HTMLTestRunner
current_path = os.path.dirname(__file__) # Get the current address
case_path = os.path.join(current_path,'unittest_case')
# Read the test methods in all modules under the directory 【 This method must use test The first method name 】
discover = unittest.defaultTestLoader.discover(start_dir=case_path,
pattern='dome*.py',
top_level_dir=None)
# print(discover)
# Create a master suite
main_suite = unittest.TestSuite()
main_suite.addTests(discover)
# now = datetime.now()
# report_file = current_path + "/test_report/time_report_{}.html".format(now)
report_file = current_path+ "/test_report/report.html"
print(report_file)
report_ojb = open(report_file, 'wb')
runner = HTMLTestRunner.HTMLTestRunner(stream=report_ojb,
title=" Baidu search test report ",
description=" Use case execution ")
# Execute the entire test suite
runner.run(main_suite)
# if __name__ == '__main__':
# unittest.main(defaultTest='main_suite')
版权声明
本文为[Short man code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220629420972.html
边栏推荐
- Observer mode -- ApplicationContext
- 通用测试技术【一】测试的分类
- Leaders of Hangzhou commercial password Application Association and their delegation visited Shiping for information exchange
- Use SSS framework to build a simple dormitory management system
- MySql查询指定一行排序到第一行
- Product test with payment function
- [Shiping information] solutions for confidentiality inspection and compliance control of recorded content
- 数据驱动pytest1
- Exploration of MySQL index
- easyui的combobox实现三级查询
猜你喜欢

.Net5中使用Swagger

Training and empowerment | building a professional technical service team
Web automation: 5.2 selenium mouse operation principle: actionchains delay call

通用测试技术【一】测试的分类

实验5 组件及事件处理

postman接口自动化-3-Tests断言3:获取动态参数,设置环境变量(接口关联)

Web problem location: F12, how to find the corresponding interface

专注数据安全,世平信息上榜中国网络安全行业全景图六大细分领域

web问题定位:F12,如何找到对应接口

jmeter 接口请求出现安全验证解决方案
随机推荐
TP5继承Base,使用base中的变量
Under the window environment, VirtualBox loads the virtual machine created in the mobile hard disk
Shiping information participated in the enterprise roadshow of Hengyang "Chuanshan forum", talked about data security and helped collaborative innovation
Web automation: 4.2 selenium how to switch iframe windows (3 methods)
MATLAB中的小技巧
Echars dynamically realizes the scatter diagram and customizes the information prompt box
monkey 实战
adb命令的使用【一】
Navicat mistakenly deleted the connection. How to restore the database
Postman intermediate: processing interface encryption and decryption AES SM3 SM4
. net5 log4net failed to log to the database after starting for a period of time
jmeter 接口请求出现安全验证解决方案
Shiping information was listed in the panorama of China's network security market in 2021
数据库原理课程设计——物资发放管理系统
laravel 新建路由文件
LDAP用户登录认证校验及查询
CefSharp存储Cookie和读Cookie
TCP三次握手和四次挥手
web问题定位:F12,如何找到对应接口
SuperSocket在.Net5中使用——AppSession和SuperSocketService篇