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'])
相关推荐
JELEE.2 小时前
Django登录注册完整代码(图片、邮箱验证、加密)
前端·javascript·后端·python·django·bootstrap·jquery
孫治AllenSun3 小时前
【算法】图相关算法和递归
windows·python·算法
QX_hao4 小时前
【Go】--反射(reflect)的使用
开发语言·后端·golang
inferno4 小时前
Maven基础(二)
java·开发语言·maven
我是李武涯5 小时前
从`std::mutex`到`std::lock_guard`与`std::unique_lock`的演进之路
开发语言·c++
史不了6 小时前
静态交叉编译rust程序
开发语言·后端·rust
读研的武6 小时前
DashGo零基础入门 纯Python的管理系统搭建
开发语言·python
Andy6 小时前
Python基础语法4
开发语言·python
但要及时清醒6 小时前
ArrayList和LinkedList
java·开发语言