天天看點

Pytest fixture 用法[email protected]( ):無參數的用法[email protected]( ):有參數的用法

[email protected]( ):無參數的用法

1.第一種方式:-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
	@pytest.fixture( )
	def user( ):
	    name = 'mixintu'
	    password = '123456'
	    return name, password

	def test_user(user):
	    assert user[0] == "mixintu"
	    assert user[1] == "123456"
	    
2.第二種方式:-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
	@pytest.fixture( )
	def user( ):
	    name = 'mixintu'
	    return name
	
	
	@pytest.fixture( )
	def password( ):
	    password = '123456'
	    return password
	
	
	def test_user(user, password):
	    assert user == "mixintu"
	    assert password == "1234567"
3.第三種方式:-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
	@pytest.fixture( )
	def user( ):
	    name = 'mixintu'
	    return name
	
	
	@pytest.fixture( )
	def pw(user):
	    name = user
	    password = '123456'
	    return name, password
	
	
	def test_user(pw):
	    assert pw[0] == 'mixintu'
	    assert pw[1] == '1234567'
  


           

輸出:

__________________________________ test_user __________________________________

	user = ('mixintu', '123456')
	
	    def test_user(user):
	        assert user[0] == "mixintu"
	>       assert user[1] == "1234567"
	E       AssertionError: assert '123456' == '1234567'
	E         - 123456
	E         + 1234567
	E         ?       +
	
	test_example.py:82: AssertionError
========================== 1 failed in 0.09 seconds ===========================
           

[email protected]( ):有參數的用法

1.第一種方式:-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
	@pytest.fixture( )  # 添加fixture後,執行inty方法就是執行fun()方法
	def inty( ):
	    return "connect to hello world"
	
	
	class TestCase_01( ):
	    # inty參數名必須同fixture的inty方法名相同才能傳遞inty的值
	    def test_open_baidu(self, inty):
	        print("============{}".format(inty))

2.第二種方式:-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
	@pytest.fixture(params = [{'userID': '00001', 'username': 'jack'},
	                          {'userID': '00002', 'username': 'mike'}])
	def getdata(request):  # 這裡的request是固定參數名
	    print("request.param======".format(request.param))
	    return request.param  # 這裡的request.param也是固定的
	
	
	class TestCase_02( ):
	    def test_case1(self, getdata):
	        print("第1個用例輸出:{}".format(getdata))
	
	    def test_case2(self, getdata):
	        print("第2個用例輸出:{}".format(getdata))

3.第三種方式:-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
	class TestCase_03( ):
	    # parametrize:直接加測試資料
	    @pytest.mark.parametrize("params_A",
	                             [{'userID': '00001', 'username': 'jack'},
	                              {'userID': '00002', 'username': 'mike'}])
	    def test_case1(self, params_A):
	        print("第1個用例輸出:{}".format(params_A))
	
	    @pytest.mark.parametrize("params_B",
	                             [{'userID': '00003', 'username': 'tina'},
	                              {'userID': '00004', 'username': 'book'}])
	    def test_case2(self, params_B):
	        print("第2個用例輸出:{}".format(params_B))

           

輸出:

Pytest fixture 用法[email protected]( ):無參數的用法[email protected]( ):有參數的用法