[Python] How do I print to console in pytest?

Discussão em 'Python' iniciado por Stack, Dezembro 7, 2024.

  1. Stack

    Stack Membro Participativo

    pytest will not print to the console when I use print. The documentation seems to say that it should work by default.

    I am using pytest my_tests.py to run this test:

    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.


    Nothing gets printed to my standard output console (just the normal progress and how many many tests passed/failed).

    And the script that I'm testing contains print:

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


    In unittest module, everything gets printed by default, which is exactly what I need. However, I wish to use pytest for other reasons.

    Does anyone know how to make the print statements get shown?

    Continue reading...

Compartilhe esta Página