[TIP] Getting Weird Coverage Reports

holger krekel holger at merlinux.eu
Sat Nov 26 04:34:56 PST 2011


On Sat, Nov 26, 2011 at 21:33 +1100, meme dough wrote:
> I will consider the implications of moving coverage start earlier, it
> could be problematic.
> 
> However, in the first instance pytest_configure is very early and
> there are 2 approaches you could take immediately.
> 
> First use pytest_sessionstart instead of pytest_configure for your
> setup doing a multicall so that cov is called before your setup.
> Without checking something along the lines of:
> 
>     def pytest_sessionstart(self,__multicall__, session):
>         __multicall__.execute()
>         # your setup here now that coverage is active

In fact, using "__multicall__" is still somewhat of an internal API and
nowadays it's better to write:

    @pytest.mark.trylast
    def pytest_sessionstart(self, session):

which should take care to execute this function as late as possible in
the chain of hook implementations.

> Second, use pytest funcargs for test fixtures.  So have a funcarg
> which only once per session (cached_setup with session scope) does the
> setup and teardown that you want.  IMHO funcargs are much better than
> xunit style test fixtures.

agreed.

> Finally, I don't see any session scope xunit style test fixture.
> Would it be worth considering adding one?  Some may point out that
> hooks like pytest_configure or pytest_sessionstart could be used for
> this purpose, but I think the purpose is actually different.  Having
> session scope test fixture (funcarg or xunit style) gives a clear
> place for test fixture code.

not sure what you mean -- funcargs allow a per-session scope, see
http://pytest.org/latest/funcargs.html?highlight=cached_setup#_pytest.python.FuncargRequest.cached_setup

cheers,
holger



More information about the testing-in-python mailing list