Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calling pytest from inside python code

Tags:

python

pytest

I am writing a Python script for collecting data from running tests under different conditions. At the moment, I am interested in adding support for Py.Test.

The Py.Test documentation clearly states that running pytest inside Python code is supported:

You can invoke pytest from Python code directly... acts as if you would call “pytest” from the command line...

However, the documentation does not describe in detail return value of calling pytest.main() as prescribed. The documentation only seems to indicate how to read the exit code of calling the tests.

What are the limits of data resolution available through this interface? Does this method simply return a string indicating the results of the test? Is support more friendly data structures supported (e.g., outcome of each test case assigned to key, value pair)?

Update: Examining the return data structure in the REPL reveals that calling pytest.main yeilds an integer return type indicating system exit code and directs a side-effect (stream of text detailing test result) to standard out. Considering this is the case, does Py.Test provide an alternate interface for accessing the result of tests run from within python code through some native data structure (e.g., dictionary)? I would like to avoid catching and parsing the std.out result because that approach seems error prone.

like image 907
David Shaked Avatar asked Dec 20 '16 22:12

David Shaked


1 Answers

I don`t think so, the official documentation tells us that pytest.main returns an os error code like is described in the example. here

You can use the pytest flags if you want to, even the traceback (--tb) option to see if some of those marks helps you.

In your other point about parsing the std.out result because that approach seems error prone. It really depends on what you are doing. Python has a lot of packages to do it like subprocess for example.

like image 175
Ederson Badeca Avatar answered Sep 20 '22 06:09

Ederson Badeca