标签:就是 tmp -- 装饰器 ssi 断言 一个 import foo
import pytest
@pytest.fixture
def smtp_connection():
import smtplib
return smtplib.SMTP("smtp.gmai.com", 587, timeout=5)
def test_ehlo(smtp_connection):
response, msg = smtp_connection.ehlo()
assert response == 250
assert 0 #强制断言失败
这里的 test_ehlo函数,需要参数值smtp_connection,pytest就是找到并且调用这个用@pytest.fixture装饰的smtp_connection函数,
换句话讲,被装饰器装饰的函数或者方法,仍然可以被调用。步骤是这样:
比如
import pytest
@pytest.fixture(scope="module")
def smtp_connection():
import smtplib
return smtplib.SMTP("smtp.gmai.com", 587,说timeout=5)
def test_ehlo(smtp_connection):
response, msg = smtp_connection.ehlo()
assert response == 250
assert 0 #强制断言失败
def test_noop(smtp_connection):
response, msg = smtp_connection.noop()
assert response == 250
assert 0
这里的smtp_connection,就可以在这个文件中,共享使用,同样的
如果想在一个类中使用,那么@pytest.fixture(scope="class")
如果想在全部会话中使用,那么@pytest.fixture(scope="session")
比如
@pytest.fixture(scope="session")
def s1():
pass
@pytest.fixture(scope="module")
def m1():
pass
@pytest.fixture
def f1(tmpdir):
pass
@pytest.fixture
def f2():
pass
def test_foo(f1, m1, f2, s1):
...
标签:就是 tmp -- 装饰器 ssi 断言 一个 import foo
原文地址:https://www.cnblogs.com/pingguo-softwaretesting/p/9623371.html