pytest的前置与后置

发布于:2025-08-19 ⋅ 阅读:(12) ⋅ 点赞:(0)
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文件

参数化

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'])

会话级别与作用

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()
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'])


网站公告

今日签到

点亮在社区的每一天
去签到