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'])
相关推荐
张子夜 iiii12 分钟前
实战项目-----Python+OpenCV 实现对视频的椒盐噪声注入与实时平滑还原”
开发语言·python·opencv·计算机视觉
2301_7703737322 分钟前
Java集合
java·开发语言
啊?啊?1 小时前
18 从对象内存到函数调用:C++ 虚函数表原理(继承覆盖 / 动态绑定)+ 多态实战
开发语言·c++·多态原理
bkspiderx1 小时前
C++标准库:文件流类
开发语言·c++
siy23331 小时前
[c语言日记] 数组的一种死法和两种用法
c语言·开发语言·笔记·学习·链表
困鲲鲲1 小时前
Flask 核心基础:从 路由装饰器 到 __name__ 变量 的底层逻辑解析
python·flask
njxiejing2 小时前
Python NumPy安装、导入与入门
开发语言·python·numpy
Rhys..2 小时前
Python&Flask 使用 DBUtils 创建通用连接池
开发语言·python·mysql
Just_Paranoid2 小时前
【Python Tkinter】图形用户界面(GUI)开发及打包EXE指南
python·gui·tkinter·pyinstaller
土了个豆子的2 小时前
04.事件中心模块
开发语言·前端·visualstudio·单例模式·c#