pytest

2015-05-15 21 views
7

adresindeki SystemExit hata iletisini veya iletiyi doğrulayın. Sızdırmazlık belgelerine göre, bir SystemExit() işleminin gerçekleştiğini, ancak daha fazlasını yapmak istediğimi söyleyebilirim: Çıkış kodunu ve herhangi bir iletiyi de doğrulamak istiyorum. Aşağıdaki kodu denedim, ancak hiçbir şey basmıyor ve doğru hata kodunu aldığımı kanıtlamak için ne yapmam gerektiğinden emin değilim.pytest

with pytest.raises(SystemExit): 
     docopt_args = validate_args(docopt_args) 
     out, err = pytest.capsys.readouterr() 
     assert out == 'Foo' 
     print out, err 

Testimi çalıştırdığım zaman geçiyor, ama işte bu. Hiçbir şey basılmaz ve ben bir iddia hatası almıyorum.

ben yürütülecek beklemek kodudur: Bu ile çalışır

 print '\n' + docopt_args['-d'] + ' is not a valid date\n' 
     sys.exit(-3) 

cevap

9

pytest son: Yapmanız gereken tek şey --capture=sys seçeneği ve dışındaki bağımlı iddiasıyla pytest çalıştırılır

raises() bağlam (bu biraz nedense önemlidir!)

Örnek:

#!/usr/bin/env python 

from __future__ import print_function 

import pytest 


def f(code=0): 
    print("Foo") 
    raise SystemExit(code) 


def test_f(capsys): 
    with pytest.raises(SystemExit): 
     f() 
    out, err = capsys.readouterr() 
    assert out == "Foo\n" 
    print(out, err) 

Demo:

$ py.test -v --capture=sys test_foo.py 
======================================= test session starts ======================================== 
platform linux2 -- Python 2.7.9 -- py-1.4.27 -- pytest-2.7.0 -- /home/prologic/.virtualenvs/test/bin/python 
rootdir: /home/prologic/tmp, inifile: 
collected 1 items 

test_foo.py::test_f PASSED 

===================================== 1 passed in 0.00 seconds ===================================== 

yılında print("Foo")print("Bar") sonuçları değiştirme: Bence

$ py.test -v --capture=sys test_foo.py 
======================================= test session starts ======================================== 
platform linux2 -- Python 2.7.9 -- py-1.4.27 -- pytest-2.7.0 -- /home/prologic/.virtualenvs/test/bin/python 
rootdir: /home/prologic/tmp, inifile: 
collected 1 items 

test_foo.py::test_f FAILED 

============================================= FAILURES ============================================= 
______________________________________________ test_f ______________________________________________ 

capsys = <_pytest.capture.CaptureFixture instance at 0x7f2729405518> 

    def test_f(capsys): 
     with pytest.raises(SystemExit): 
      f() 
     out, err = capsys.readouterr() 
>  assert out == "Foo\n" 
E  assert 'Bar\n' == 'Foo\n' 
E   - Bar 
E   + Foo 

test_foo.py:17: AssertionError 
===================================== 1 failed in 0.01 seconds ===================================== 

sen sonra ne tam olarak!

Temiz bir virtualenv bu yaptı:

mkvirtualenv test 
pip install pytest 

burada hüner Aptalca bir kullanıcı hatası olduğunu okuyup Setting capturing methods or disabling capturing

+0

anlamaktır .... ben girinti yanlış vardı. Sağlanan örneğe baktığımda bunu fark ettim. –

+0

Ahh evet bundan bahsetmeyi unuttum :) Cevabımı güncellememe izin verin :) –

+3

Varolan kodla ilgili iddialarda bulunmak için şu gibi bir şeye ihtiyacınız var: "pytest.raises (SystemExit) ile excinfo olarak: assert excinfo.value.code == 1 " – mvr

0
import pytest 

def test_exit(): 
    with pytest.raises(SystemExit): 
     script_exit() 
İlgili konular