天天看點

12.python開源——pytest自動化測試架構

1      下載下傳連結

https://docs.pytest.org/en/latest/getting-started.html

或者使用pip安裝

指令如下:

pip install pytest

更新指令:pip install -u pytest

檢視版本pytest —version

2      pytest使用

建立test_func.py

内容如下:

def func(x):return x + 1

def test_answer(): assert func(3) == 5

然後執行

#pytest test_func.py

collected 1items

test_func.py f

===================================failures ===================================

_________________________________test_answer __________________________________

>  def test_answer(): assert func(3) == 5

e  assert 4 == 5

e   +  where 4 = func(3)

test_func.py:3: assertionerror

=========================== 1 failedin 0.02 seconds ===========================

測試結束,該用例測試失敗。

因為函數結果因為4,不等于5嘛。

        pytest會在目前目錄中運作所有檔案以及子檔案夾,需要以test_*.py或*_test.py檔案。遵循标準規則。

-q參數表示quiet

建立test_class.py内容如下:

class testclass:

    def test_one(self):

            x = "this"

            assert 'h' in x

    def test_two(self):

            x = "hello"

            assert hasattr(x, 'check')

 測試結果如下:

1failed, 1 passed in 0.02 seconds

建立test_temp.py内如如下:

def test_needsfiles(tmpdir):

    print (tmpdir)

assert 0

然後執行,會報錯

在測試函數中列出了tmpdir。

# pytest --fixtures