天天看點

pytest學習--fixture

import pytest
import smtplib
import contextlib


# @pytest.fixture(scope='function')
# def smtp_connection():
#     return smtplib.SMTP("smtp.gmail.com", 587, timeout=5)
#
#
# '''
#     pytest --fixtures test_simplefactory.py
#     檢視可用的燈具(帶字首的燈具_僅在添加-v選項後才會顯示)
# '''
#
#
# def test_ehlo(smtp_connection):
#     response, msg = smtp_connection.ehlo()
#     print('*'*50)
#     print(response, msg, end=' ')
#     print('*' * 50)
#     assert response == 250
#     assert 0  # for demo purposes
'''
     fixture(scope="function", params=None, autouse=False, ids=None, name=None)
        scope有四個級别的參數: function, class, Module, session
        params:一個可選的參數清單,它将導緻多個參數調用fixture功能和所有測試使用它
         autouse:如果為True,則為所有測試激活fixture func可以看見他,如果為False(預設值)
        則顯示需要參考來激活fixture
        每個字元串id的清單, 每個字元串對應于params這樣他們就是測試ID的一部分。如果沒有提供ID
        他們從params自動生成
        name:fixture的名稱 這預設是裝飾函數的名稱
'''



# @pytest.fixture()預設是函數級别的 @pytest.fixture(scope="function")

# @python.fixture()函數級别次序
order = []


@pytest.fixture(scope="session")
def s1():
    order.append("session")
    assert 1


@pytest.fixture(scope="module")
def m1():
    order.append("module")
    assert 1


@pytest.fixture(scope="function")
def f1():
    order.append("function")
    assert 1


def test_order_list(s1, m1, f1):
    assert order == ['session', 'module', 'function']


@pytest.fixture(scope="module")
def login():
    """
        通過使用yield語句代替return,yield語句之後的所有代碼都将用作結束調用的拆卸語句:
    :yield:
    """
    print("輸入賬号密碼:")
    yield login
    print("我忘記了")


def test_s1(login):
    print("user:{},password:{}".format('Mike', '123456'))
    assert 1


# 無論夾具設定代碼是否引發異常,都将始終使用contextlib.ExitStack上下文管理器終結器進行調用
@contextlib.contextmanager
def connect(port):
    print("引用{}".format(port))
    yield connect()
    print("關閉引用了")


@pytest.fixture(scope='function')
def equipments():
    with contextlib.ExitStack() as stack:
        yield [stack.enter_context(connect(port)) for port in ("C1", "C3", "C28")]
        """
            請注意,我們還可以将yield文法與with語句無縫結合使用
            如果"C28"失敗,異常,"C1"并且"C3"将仍然被正确關閉
        """
# def setup_module():
#     print("子產品級别開始")
#
#
# def teardown_module():
#     print("子產品級别結束")
#
#
# def setup_function():
#     print("函數級别開始")
#
#
# def teardown_function():
#     print("函數級别結束")
#
#
# def test_print1():
#     print("輸出用例1")
#
#
# def test_print2():
#     print("輸出用例2")


# class TestMethod:
#     def setup_class(self):
#         print("類對象開始")
#
#     def teardown_class(self):
#         print("類對象結束")
#
#     def setup_method(self):
#         print("類對象方法開始")
#
#     def teardown_method(self):
#         print("類對象方法結束")
#
#     def setup(self):
#         print("setup開始")
#
#     def teardown(self):
#         print("teardown結束")
#
#     def test_1(self):
#         print('1')


# def setup_module():
#     print("子產品級别開始")
#
#
# def teardown_module():
#     print("子產品級别結束")
#
#
# def setup_function():
#     print("函數級别開始")
#
#
# def teardown_function():
#     print("函數級别結束")
#
#
# def test_print1():
#     print("輸出用例1")
#
#
# def test_print2():
#     print("輸出用例2")


# 參數化
@pytest.fixture(scope='module', params=['I am here', 'always waiting for you'])
def print_here(request):
    print(request.param)
    assert 1


def test_task1(print_here):
    print("這是參數化測試")
    assert 1


@pytest.fixture(params=[0, 1], ids=["spam", "ham"])
def a(request):
    return request.param


def test_a(a):
    pass


def idfnc(fixture_value):
    if fixture_value == 0:
        return "eggs"
    else:
        return None


@pytest.fixture(params=[0, 1], ids=idfnc)
def b(request):
    return request.param


def test_b(b):
    pass