2014-07-07 3 views
57

मैं pytest मॉड्यूल के साथ टेस्ट-संचालित विकास का उपयोग करने की कोशिश कर रहा हूं। जब मैं print लिखता हूं तो pytest print कंसोल पर नहीं होगा।पीई टेस्ट में कंसोल पर प्रिंट कैसे करें?

मैं py.test my_tests.py का उपयोग इसे चलाने के लिए ...

documentation कहना है कि यह डिफ़ॉल्ट रूप से काम करना चाहिए लगता है: http://pytest.org/latest/capture.html

लेकिन:

import myapplication as tum 

class TestBlogger: 

    @classmethod 
    def setup_class(self): 
     self.user = "alice" 
     self.b = tum.Blogger(self.user) 
     print "This should be printed, but it won't be!" 

    def test_inherit(self): 
     assert issubclass(tum.Blogger, tum.Site) 
     links = self.b.get_links(posts) 
     print len(links) # This won't print either. 

कुछ भी नहीं है मेरी मानक आउटपुट में मुद्रित हो जाता है कंसोल (केवल सामान्य प्रगति और कितने परीक्षण पास/असफल)।

और स्क्रिप्ट है कि मैं परीक्षण कर रहा हूँ प्रिंट शामिल हैं:

class Blogger(Site): 
    get_links(self, posts): 
     print len(posts) # It won't get printed in the test. 

unittest में मॉड्यूल, सब कुछ डिफ़ॉल्ट रूप से मुद्रित हो जाता है, जो कि मैं वास्तव में क्या जरूरत है। हालांकि, मैं अन्य कारणों से pytest का उपयोग करना चाहता हूं। ऐसा लगता है कि ऐसी बुनियादी कार्यक्षमता है कि शायद मैं इसे याद कर रहा हूँ !?

क्या कोई जानता है कि प्रिंट स्टेटमेंट कैसे दिखाए जाएंगे?

+0

शायद stdout ओवरराइट किया जा रहा है। यदि आप 'sys.stdout.write (" test ") का उपयोग करते हैं तो क्या होता है? 'Sys .__ stdout __ के बारे में कैसे लिखें (" टेस्ट ")'? उत्तरार्द्ध हमेशा सिस्टम-परिभाषित stdout को लिखना चाहिए, जो कंसोल होना चाहिए। यदि दो आदेश अलग-अलग चीजें करते हैं, तो stdout बदल रहा है; अगर वे एक ही काम करते हैं, तो समस्या कुछ और है। – TheSoundDefense

उत्तर

77

डिफ़ॉल्ट रूप से, py.test मानक आउटपुट के परिणाम को कैप्चर करता है ताकि यह नियंत्रित हो सके कि यह इसे कैसे प्रिंट करता है। अगर ऐसा नहीं होता है, तो उस पाठ को मुद्रित किए गए परीक्षण के संदर्भ के बिना यह बहुत सारे पाठ को बाहर कर देगा।

हालांकि, यदि कोई परीक्षण विफल रहता है, तो इसमें परिणामी रिपोर्ट में एक अनुभाग शामिल होगा जो दिखाता है कि उस विशेष परीक्षण में मानक को मुद्रित किया गया था।

उदाहरण के लिए

,

निम्नलिखित उत्पादन में
def test_good(): 
    for i in range(1000): 
     print(i) 

def test_bad(): 
    print('this should fail!') 
    assert False 

परिणाम:

>>> py.test tmp.py 
============================= test session starts ============================== 
platform darwin -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2 
plugins: cache, cov, pep8, xdist 
collected 2 items 

tmp.py .F 

=================================== FAILURES =================================== 
___________________________________ test_bad ___________________________________ 

    def test_bad(): 
     print('this should fail!') 
>  assert False 
E  assert False 

tmp.py:7: AssertionError 
------------------------------- Captured stdout -------------------------------- 
this should fail! 
====================== 1 failed, 1 passed in 0.04 seconds ====================== 

नोट Captured stdout अनुभाग।

यदि आप print कथन को निष्पादित करते समय देखना चाहते हैं, तो आप -s ध्वज py.test पर भेज सकते हैं। हालांकि, ध्यान दें कि कभी-कभी इसे पार्स करना मुश्किल हो सकता है।

>>> py.test tmp.py -s 
============================= test session starts ============================== 
platform darwin -- Python 2.7.6 -- py-1.4.20 -- pytest-2.5.2 
plugins: cache, cov, pep8, xdist 
collected 2 items 

tmp.py 0 
1 
2 
3 
... and so on ... 
997 
998 
999 
.this should fail! 
F 

=================================== FAILURES =================================== 
___________________________________ test_bad ___________________________________ 

    def test_bad(): 
     print('this should fail!') 
>  assert False 
E  assert False 

tmp.py:7: AssertionError 
====================== 1 failed, 1 passed in 0.02 seconds ====================== 
19

-s विकल्प का उपयोग सभी कार्यों, बहुत ज्यादा हो सकता है, जिनमें से उत्पादन प्रिंट होगा। अपने समारोह के अंत में

  1. सम्मिलित assert False, "dumb assert to make PyTest print my stuff", और आप विफल परीक्षण की वजह से अपने उत्पादन देखेंगे:

    आप विशेष उत्पादन की जरूरत है, तो डॉक पेज आप का उल्लेख कुछ सुझाव प्रदान करता है।

  2. आप विशेष वस्तु PyTest द्वारा पारित किया है, और आप इसे बाद में निरीक्षण करने के लिए, की तरह

    def test_good1(capsys): 
        for i in range(5): 
         print i 
        out, err = capsys.readouterr() 
        open("err.txt", "w").write(err) 
        open("out.txt", "w").write(out) 
    

    आप एक अलग टैब में out और err फ़ाइलें खोल सकते हैं एक फ़ाइल में उत्पादन लिख सकते हैं और संपादक को स्वचालित रूप से इसे आपके लिए रीफ्रेश करने दें, या अपना परीक्षण चलाने के लिए एक सरल py.test; cat out.txt खोल कमांड करें।

बल्कि चीज़ें करने hackish तरीका है, लेकिन हो सकता है यह सामान आप की जरूरत है कि: सब के बाद, TDD आप गड़बड़ सामग्री के साथ मतलब है और यह साफ है और चुप छोड़ जब यह तैयार :-) है।

5

मुझे PyTest म्यूट शाब्दिक सब कुछ पर स्किप किए गए परीक्षणों के बारे में महत्वपूर्ण चेतावनी मुद्रित करने की आवश्यकता है।

मैं एक परीक्षण विफल एक संकेत भेजने के लिए नहीं करना चाहता था, इसलिए मैं इस प्रकार एक हैक किया:

def test_2_YellAboutBrokenAndMutedTests(): 
    import atexit 
    def report(): 
     print C_patch.tidy_text(""" 
In silent mode PyTest breaks low level stream structure I work with, so 
I cannot test if my functionality work fine. I skipped corresponding tests. 
Run `py.test -s` to make sure everything is tested.""") 
    if sys.stdout != sys.__stdout__: 
     atexit.register(report) 

atexit मॉड्यूल मुझेPyTest के बाद सामान मुद्रित करने के लिए जारी किया गया आउटपुट स्ट्रीम अनुमति देता है। आउटपुट के रूप में पालन दिखता है:

============================= test session starts ============================== 
platform linux2 -- Python 2.7.3, pytest-2.9.2, py-1.4.31, pluggy-0.3.1 
rootdir: /media/Storage/henaro/smyth/Alchemist2-git/sources/C_patch, inifile: 
collected 15 items 

test_C_patch.py .....ssss....s. 

===================== 10 passed, 5 skipped in 0.15 seconds ===================== 
In silent mode PyTest breaks low level stream structure I work with, so 
I cannot test if my functionality work fine. I skipped corresponding tests. 
Run `py.test -s` to make sure everything is tested. 
~/.../sources/C_patch$ 

संदेश छपा है, तब भी जब PyTest चुप मोड में है, और मुद्रित नहीं आप py.test -s साथ सामान चलाते हैं है, तो सब कुछ पहले से ही अच्छी तरह से परीक्षण किया जाता है।

1

pytest docs, pytest --capture=sys के अनुसार काम करना चाहिए। यदि आप किसी परीक्षण के अंदर मानक को कैप्चर करना चाहते हैं, तो कैप्सिस फ़िक्स्चर देखें।

संबंधित मुद्दे