How to test?
Manfred Lotz
ml_news at posteo.de
Mon Jun 22 01:14:56 EDT 2020
On Wed, 17 Jun 2020 17:34:55 +0100
Tony Flury <tony.flury at btinternet.com> wrote:
> On 24/04/2020 19:40, Manfred Lotz wrote:
> > I have a command like application which checks a directory tree for
> > certain things. If there are errors then messages will be written to
> > stdout.
> >
> > How to test this in the best way?
> >
> > One idea was for the error situations to write messages to files and
> > then later when running the tests to compare the error messages
> > output to the previously saved output.
> >
> > Is there anything better?
> >
>
> In a recent application that I wrote (where output to the console was
> important), I tested it using the 'unittest' framework, and by
> patching sys.stderr to be a StringIO - that way my test case could
> inspect what was being output.
>
> with patch('sys.stderr', StringIO()) as
> stderr:application.do_stuff()self.assertTrue(stderr.getvalue(),
> 'Woops - that didn\'t work')
>
> I am not sure of the structure of your application, and whether you
> have a callable API that you can invoke.
>
Thanks a lot. That's really great.
In the meantime I switched to pytest because things are so much more
convenient to setup. So, also thanks to the others who gave the links to
how this can be done with pytest.
--
Manfred
More information about the Python-list
mailing list