いくつかの便利なプラグインの練習pytest

1.複数のチェックpytest-仮定

あなたが複数のアサートを書くことができますが、単純なチェックサムアサート、

    def test_add1(self):
        assert add(2,3)==5 assert add(1,3)==3 assert add(2,5)==7 

第二のアサーションが失敗したので、次のアサーションは実行されません。
あなたはより多くのアサーションを必要とするのであれば、我々は、サードパーティ製のプラグインのpytestは、前提と実行する必要が
インストールコマンドを:

pip install pytest-assume

例:

    def test_add2(self):
        pytest.assume(add(1,2)==3) pytest.assume(add(1,4)==3) pytest.assume(add(2,2)==4) 

ここでも、第二アサーションは失敗し、第三の主張はまだ継続されます。

 

2.実行順序pytest-順序を設定します。

一部のコンテキスト依存のために、そして時にはプラグpytestを注文、実行のいくつかの特定の順序を設定する必要があり、それがこの問題に良い解決策である
コマンドをインストール

pip  install  pytest-ordering

サンプルスクリプトは次のよう:

def test_order1(): print ("first test") assert True def test_order2(): print ("second test") assert True 

示すように、発注、実行test_order1の順---- test_order2を追加せずに:

 

 
before.png

のオーダー後

@pytest.mark.run(order=2) def test_order1(): print ("first test") assert True @pytest.mark.run(order=1) def test_order2(): print ("second test") assert True 

結果は以下の通りであります:

 

 
after.png

重いpytest-rerunfailuresを実行するために5.失敗

激しい走行での患者による失敗は、個人的にこれは非常に便利なプラグインであることを感じています。
例えば:
、重いランと組み合わせた場合、いくつかの要素は、故障のクリックで、その結果、示すことができなかった遭遇などselenuimやappiumとして、UIの自動化を行う際には、レポートの精度が向上します。
インストールコマンド:

pip install pytest-rerunfailures 

サンプルスクリプト

class TestRerun(): @pytest.mark.run(order=2) @pytest.mark.flaky(reruns=5) def test_random(self): print(1) pytest.assume((random.randint(0,4)+3)==5) @pytest.mark.run(order=3) def test_random2(self): ''' 不加mark 命令行中 pytest -sq demo_rerun.py --reruns 5 :return: ''' print(2) pytest.assume((random.randint(0,4)+3)==5) @pytest.mark.run(order=1) @pytest.mark.flaky(reruns=6, reruns_delay=2) def test_example(self): print(3) assert random.choice([True, False])

コマンドを実行します。

pytest -sq demo_rerun.py --reruns 5
pytest -sq demo_rerun.py --reruns 5  --reruns-delay 1

違いは、それぞれの再実行は1秒間待機します前に、以下の、である
あなたはまた、実行時に、スクリプトでこの時間を再定義し、実行の数を指定することができ、同時に、あなたはこのパラメータ--rerunsを追加する必要はありません

@pytest.mark.flaky(reruns=6, reruns_delay=2) def test_example(self): print(3) assert random.choice([True, False]) 
 

コントラスト見ることができ、実行順序が変更されました

おすすめ

転載: www.cnblogs.com/peng-lan/p/11511569.html