pytest的前置与后置

python 复制代码
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import time
#登录成功用例
class TestCase:
    def setup_method(self):   #此处为前置,setup和teardown用的少一般用fixture做前置后置
        self.driver = webdriver.Chrome()
        self.wait = WebDriverWait(self.driver, 10)
        self.driver.get("http://novel.hctestedu.com/user/login.html")
    def teardown_method(self):#后置
        time.sleep(3)
        self.driver.quit()

    def test_login_success(self):
        self.driver.find_element(By.ID, "txtUName").send_keys("13587545934")
        self.driver.find_element(By.ID, "txtPassword").send_keys("q20050821")
        # 判断是否改变地址
        login_url = self.driver.current_url
        self.driver.find_element(By.ID, "btnLogin").click()
        time.sleep(2)
        sjmsg = self.wait.until(ec.url_changes(login_url))
        assert sjmsg== True , 'Login fail'
        #密码失败:
    def test_loginFail(self):

        self.driver.find_element(By.ID, "txtUName").send_keys("13587545934")
        self.driver.find_element(By.ID, "txtPassword").send_keys("20050821")
        # 判断是否改变地址
        login_url = self.driver.current_url
        self.driver.find_element(By.ID, "btnLogin").click()
        time.sleep(2)
        sjmsg = self.wait.until(ec.url_changes(login_url))
        assert sjmsg == True , 'Login fail'
#账号失败:
    def test_loginname(self):
        self.driver.find_element(By.ID, "txtUName").send_keys("113587545934")
        self.driver.find_element(By.ID, "txtPassword").send_keys("q20050821")
        # 判断是否改变地址
        login_url = self.driver.current_url
        self.driver.find_element(By.ID, "btnLogin").click()
        sjmsg = self.wait.until(ec.url_changes(login_url))
        assert sjmsg == True , 'Login fail'
if __name__ =="__main__":
    pytest.main(["-sv",'case_test.py'])

#module就是模块级别的函数条件,等于一个py文件

参数化

python 复制代码
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
import time
#参数化@pytest.mark.parametrize
class TestCase:
    def setup_method(self):#setup和teardown用的少一般用fixture做前置后置
        self.driver = webdriver.Chrome()
        self.wait = WebDriverWait(self.driver, 10)
        self.driver.get("http://novel.hctestedu.com/user/login.html")
    def teardown_method(self):
        time.sleep(3)
        self.driver.quit()

    @pytest.mark.parametrize("data",[{'user':13587545934,'pwd':'q20050821'},{'user':'13587545933', 'pwd':'q20050821'}])
    def test_login_success(self,data):
        self.driver.find_element(By.ID, "txtUName").send_keys(data['user'])
        self.driver.find_element(By.ID, "txtPassword").send_keys(data['pwd'])
        # 判断是否改变地址
        login_url = self.driver.current_url
        self.driver.find_element(By.ID, "btnLogin").click()
        time.sleep(2)
        sjmsg = self.wait.until(ec.url_changes(login_url))
        assert sjmsg== True , 'Login fail'
        #密码失败:

if __name__ =="__main__":
    pytest.main(["-sv",'mark_test.py'])

会话级别与作用

python 复制代码
import pytest
from selenium import webdriver

import time

# @pytest.fixture(scope='function')#function级别会一个一个打开网页 class同理
# def browser():
#     driver=webdriver.Chrome()
#     yield driver
#     driver.quit()
@pytest.fixture(scope='session')#session级别会在一个浏览器上打开所有网页 module同理
def browser():
    driver=webdriver.Chrome()
    yield driver
    driver.quit()
python 复制代码
import pytest


def test_taobao(browser):
    browser.get('https://www.taobao.com/')
    assert 1==1
def test_baidu(browser):
    browser.get('https://www.baidu.com/')
    assert  1==2

if __name__ == '__main__':
    pytest.main(['-vs','wy_test.py'])
相关推荐
楼田莉子22 分钟前
C++学习:C++11介绍及其新特性学习
开发语言·c++·学习·stl·visual studio
Lululaurel25 分钟前
深度模型瘦身术:从100MB到5MB的工业级压缩实战
pytorch·python·机器学习·模型压缩·模型优化·边缘部署
不枯石1 小时前
Matlab通过GUI实现点云的随机一致性(RANSAC)配准
开发语言·图像处理·算法·计算机视觉·matlab
牛马的人生1 小时前
MATLAB模块库入门:提升你的工程分析效率
开发语言·其他·matlab
那雨倾城2 小时前
PiscCode:基于OpenCV的前景物体检测
图像处理·python·opencv·计算机视觉
光电笑映3 小时前
C++list全解析
c语言·开发语言·数据结构·c++·list
一粒马豆3 小时前
flask_socketio+pyautogui实现的具有加密传输功能的极简远程桌面
python·flask·pyautogui·远程桌面·flask_socketio
恋猫de小郭3 小时前
Fluttercon EU 2025 :Let‘s go far with Flutter
android·开发语言·flutter·ios·golang
小龙报4 小时前
《构建模块化思维---函数(下)》
c语言·开发语言·c++·算法·visualstudio·学习方法
一只学java的小汉堡4 小时前
Spring Cloud RabbitMQ 详解:从基础概念到秒杀实战
开发语言·后端·ruby