test/public :是登录和退出模块, 注意:_init_.py 是必须要的
test:test_cheshi2,,,test_cheshi3 这两个分别是不同的测试用例
all_case 执行所有用例
本次用例功能点:登录,退出,用例,分别模块化,总的文件来执行所有的用例,登录和退出是公共模块,所有放在一个文件夹,
后续用例需要就直接调用即可,
all_case : 在unittest的基础上,引用了pytest框架,注意下文的第二点,如果要生成测试报告的话:只能通过cmd命令里执行。进入这个项目下,
然后输出;pytest --html=report.html 比如当前:cd C:\Users\Admin\PycharmProjects\测试模块 pytest --html=report.html
1.unittest提供了test cases、test suites、test fixtures、test runner相关的类,让测试更加明确、方便、可控。使用unittest编写用例,必须遵守以下规则:
(1)测试文件必须先import unittest
(2)测试类必须继承unittest.TestCase
(3)测试方法必须以“test_”开头
(4)测试类必须要有unittest.main()方法
2.pytest是python的第三方测试框架,是基于unittest的扩展框架,比unittest更简洁,更高效。使用pytest编写用例,必须遵守以下规则:
(1)测试文件名必须以“test_”开头或者"_test"结尾(如:test_ab.py)
(2)测试方法必须以“test_”开头。
(3)测试类命名以"Test"开头
all_case.py
#coding=utf-8
import unittest
#这里需要导入测试文件
import sys
sys.path.append("\test")
import sys
sys.path.append('\report')
from test import *
import pytest
if __name__ == '__main__':
pytest.main()
test_ceshi2.py
#coding=utf-8
import time
import unittest
import sys
sys.path.append("\\public")
#导入登录,退出模块
from test.public import test_login
from test.public import test_exit
from selenium import webdriver
#测试用例1
class Test_cheshi2():
#登录
def test_cheshi2(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.driver.base_url = '自己的网址'
print('ctt')
driver = self.driver
driver.get(driver.base_url)
# 窗口最大化
self.driver.maximize_window()
#调用登录模块
test_login.login(self)
# 新功能引导
# 通过两层frame方式定位
driver.switch_to.frame("id_iframe")
driver.find_element_by_xpath('//*[@id="jpwClose"]').click()
time.sleep(5)
# 进入实验室管理系统 吸取的教训2:始终定位不成功的时候可以借助火狐浏览器的插件,比如:katalon 定位一遍,复制定位到的元素
driver.find_element_by_xpath("(//div[@id='MENU_SYSGL']/span[2])[2]").click()
time.sleep(2)
# 退出两层frame方式定位
driver.switch_to.default_content()
#调用退出模块
test_exit.tearDown(self)
test_cheshi3.py
#coding=utf-8
import time
import unittest
import sys
sys.path.append("\\public")
#导入登录,退出模块
from test.public import test_login
from test.public import test_exit
from selenium import webdriver
#测试用例2
class Test_cheshi3():
#登录
def test_cheshi3(self):
self.driver = webdriver.Firefox()
self.driver.implicitly_wait(30)
self.driver.base_url = '自己的网址'
driver = self.driver
driver.get(driver.base_url)
# 窗口最大化
self.driver.maximize_window()
#调用登录模块
test_login.login(self)
# 新功能引导
# 通过两层frame方式定位
driver.switch_to.frame("id_iframe")
driver.find_element_by_xpath('//*[@id="jpwClose"]').click()
time.sleep(5)
# 进入基础管理 吸取的教训2:始终定位不成功的时候可以借助火狐浏览器的插件,比如:katalon 定位一遍,复制定位到的元素
driver.find_element_by_xpath('/html/body/div[3]/div[4]/div/div[1]/div[2]/span[2]').click()
time.sleep(2)
# 退出两层frame方式定位
driver.switch_to.default_content()
#调用退出模块
test_exit.tearDown(self)
test_exit.py
#coding=utf-8
from selenium import webdriver
#退出模块
def tearDown(self):
self.driver.quit()
test_login.py
#coding=utf-8
from selenium import webdriver
import time
#登录模块
def login(self):
driver = self.driver
print('ctt')
driver.find_element_by_id('showAccount').click()
driver.find_element_by_id('userAccount').send_keys('ctt')
driver.find_element_by_id('showPassword').click()
driver.find_element_by_id('userPassword').send_keys('123456')
driver.find_element_by_id('dengluBtn').click()
time.sleep(5)
__init__.py 路径在test\__init__.py
import test.test_cheshi2
import test.test_cheshi3
目录结构