1.setup_class 和teardown_class用法
# coding=utf-8
import pytest
from selenium import webdriver
import time
class Test(object):
@classmethod
def setup_class(cls):
print("初始化浏览器")
cls.driver=webdriver.Chrome()
cls.driver.maximize_window()
time.sleep(2)
cls.driver.get("http://www.baidu.com")
@classmethod
def teardown_class(cls):
cls.driver.close()
print("关闭浏览器")
def test_in01(self):
self.driver.find_element_by_id("kw").send_keys("流浪的python")
self.driver.find_element_by_id("su").click()
print("0001 is ok")
def test_in02(self):
self.driver.find_element_by_xpath('//*[@id="s_tab"]/div/a[1]').click()
time.sleep(3)
print("002 is ok ")
============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: D:\Pycharm\PyCharm 5.0.4\jre\jre\bin, inifile:
plugins: xdist-1.26.0, rerunfailures-6.0, metadata-1.8.0, html-1.20.0, forked-1.0.1, allure-pytest-2.5.4
collected 2 items
. 初始化浏览器
0001 is ok
.002 is ok
.关闭浏览器
2.setup_method 和teardown_method 用法
class TestMethod(object):
def setup_method(self):
print("初始化浏览器")
self.driver=webdriver.Chrome()
self.driver.maximize_window()
self.driver.get("http://www.baidu.com")
def teardown_method(self):
self.driver.close()
print("关闭浏览器")
def test_in01(self):
self.driver.find_element_by_id("kw").send_keys("流浪的python")
self.driver.find_element_by_id("su").click()
print("第一次打开浏览器is ok")
def test_in02(self):
self.driver.find_element_by_id("kw").send_keys("流浪的python")
self.driver.find_element_by_id("su").click()
self.driver.find_element_by_xpath('//*[@id="s_tab"]/div/a[1]').click()
time.sleep(3)
print("第二次打开浏览器 is ok ")
============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: D:\Pycharm\PyCharm 5.0.4\jre\jre\bin, inifile:
plugins: xdist-1.26.0, rerunfailures-6.0, metadata-1.8.0, html-1.20.0, forked-1.0.1, allure-pytest-2.5.4
collected 2 items
. 初始化浏览器
第一次打开浏览器is ok
.关闭浏览器
初始化浏览器
第二次打开浏览器 is ok
.关闭浏览器
3. pytest.fixture用法介绍
# coding=utf-8
import pytest
from selenium import webdriver
from pyfoo.Utils.pageobj import GetEle
from pyfoo.Utils.pageobj import *
@pytest.fixture(scope="class")
def driver():
url="http://www.baidu.com"
driver= webdriver.Chrome()
driver.get(url)
print("初始化完成...")
yield driver
driver.close()
print("teardown...关闭浏览器")
# @pytest.mark.usefixtures("driver")
class Test_fix_module(object):
def test_open_bd(self,driver):
GetEle().ele_by_id(driver,'kw').send_keys("pytest")
GetEle().ele_by_id(driver,'su').click()
print("搜索资源case成功")
def test_open_tb(self,driver):
GetEle().ele_by_xpath(driver,'//*[@id="s_tab"]/div/a[1]').click()
print("进入资讯case成功")
if __name__ == "__main__":
pytest.main(['-v','-s','-q'])
============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: D:\Pycharm\PyCharm 5.0.4\jre\jre\bin, inifile:
plugins: xdist-1.26.0, rerunfailures-6.0, metadata-1.8.0, html-1.20.0, forked-1.0.1, allure-pytest-2.5.4
collected 2 items
. 初始化完成...
搜索资源case成功
.进入资讯case成功
.teardown...关闭浏览器
4.setup_module 和teardown_module
# coding=utf-8
import pytest
from selenium import webdriver
import time
def setup_module():
global driver
driver=webdriver.Chrome()
driver.maximize_window()
time.sleep(2)
driver.get("http://www.baidu.com")
print("开始模块初始化浏览器")
def teardown_module():
driver.close()
print("模块执行结束关闭浏览器")
class TestModule(object):
def test_001(self):
driver.find_element_by_id("kw").send_keys("流浪的python")
driver.find_element_by_id("su").click()
print("test_001 is ok ")
def test_002(self):
driver.find_element_by_xpath('//*[@id="s_tab"]/div/a[1]').click()
time.sleep(3)
print("test_002 is ok ")
============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: D:\Pycharm\PyCharm 5.0.4\jre\jre\bin, inifile:
plugins: xdist-1.26.0, rerunfailures-6.0, metadata-1.8.0, html-1.20.0, forked-1.0.1, allure-pytest-2.5.4
collected 2 items
. 开始模块初始化浏览器
test_001 is ok
.test_002 is ok
.模块执行结束关闭浏览器
5.setup_function 和 teardown_function
# coding=utf-8 import pytest from selenium import webdriver import time def setup_function(): print("setup_function():每个函数之前执行") def teardown_function(): print ("teardown_function():每个函数之后执行") def test_01(): print ("正在执行函数test1") x = "this" assert 'h' in x def add(a,b): return a+b def test_add(): print ("正在执行test_add()") assert add(3,4) == 7
============================= test session starts =============================
platform win32 -- Python 3.7.2, pytest-4.1.1, py-1.7.0, pluggy-0.8.1
rootdir: D:\Pycharm\PyCharm 5.0.4\jre\jre\bin, inifile:
plugins: xdist-1.26.0, rerunfailures-6.0, metadata-1.8.0, html-1.20.0, forked-1.0.1, allure-pytest-2.5.4
collected 2 items
. setup_function():每个函数之前执行
正在执行函数test1
.teardown_function():每个函数之后执行
setup_function():每个函数之前执行
正在执行test_add()
.teardown_function():每个函数之后执行