pytest框架(五)

代码示例一

 1 # coding=utf-8
 2 import pytest
 3 
 4 
 5 @pytest.fixture()
 6 def login():
 7     print("输入账号,密码先登录")
 8 
 9 
10 def test_s1(login):
11     print("用例1:登录之后其他动作111")
12 
13 
14 def test_s2():  # 不传login
15     print("用例2:不需要登录,操作222")
16 
17 
18 def test_s3(login):
19     print("用例3:登录之后其他动作333")
20 
21 
22 if __name__ == "__main__":
23     pytest.main(['-s', 'test_fixture.py'])

代码示例二

conftest.py

#coding=utf-8
import pytest

@pytest.fixture()
def login():
    print("输入账号密码,登录")

test_fix1.py

# coding=utf-8
import pytest


def test_s1(login):
    print("用例1:登录之后其它动作111")


def test_s2():  # 不传login
    print("用例2:不需要登录,操作222")


def test_s3(login):
    print("用例3:登录之后其它动作333")


if __name__ == "__main__":
    pytest.main(["-s", "test_fix1.py"])

test_fix2.py

# coding:utf-8
import pytest


def test_s4(login):
    print("用例4:登录之后其它动作444")


def test_s5():  # 不传login
    print("用例5:不需要登录,操作555")


if __name__ == "__main__":
    pytest.main(["-s", "test_fix2.py"])
原文地址:https://www.cnblogs.com/loveapple/p/9527940.html