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'])
相关推荐
阿巴~阿巴~几秒前
深入解析C++非类型模板参数
开发语言·c++
集成显卡2 小时前
使用 Google 开源 AI 工具 LangExtract 进行结构化信息抽取
python·google·openai
久笙&2 小时前
对象存储解决方案:MinIO 的架构与代码实战
数据库·python·架构
小指纹2 小时前
河南萌新联赛2025第(六)场:郑州大学
java·开发语言·数据结构·c++·算法
不甘懦弱3 小时前
阿里云搭建flask服务器
服务器·python·flask
赵英英俊3 小时前
Python day51
人工智能·pytorch·python
飞翔的佩奇3 小时前
【完整源码+数据集+部署教程】遥感森林砍伐检测系统源码和数据集:改进yolo11-SWC
python·yolo·计算机视觉·数据集·yolo11·遥感森林砍伐检测
阿汤哥的程序之路3 小时前
Python如何将两个列表转化为一个字典
python
山中月侣4 小时前
Java多线程编程——基础篇
java·开发语言·经验分享·笔记·学习方法