[pytest-dev] Creating a complex PyTest program, wondering about the best way to handle collection errors, etc.

Floris Bruynooghe flub at devork.be
Fri Apr 20 15:50:03 EDT 2018


On Thu 19 Apr 2018 at 16:50 -0500, Nicholas Williams wrote:

> The tests work this way:
>
> - There's a test class that inherits from our specialized test class
> (ServicePlanTestCase), which inherits from `unittest.TestCase` (for several
> reasons, this detail cannot be altered)
> - That test class can do setup and teardown like a normal test class, but
> it has a static attribute that points to a directory
> - That directory contains one or more files ending in a particular
> extension, and those files each contain one or more tests defined using a
> particular syntax

I'm not even going to ask how this all came to be this way... but thanks
for giving enough context.

> Based on the hints you gave me, it sounds like I could do something like
> this:
> https://github.com/pytest-dev/pytest/blob/4678cbeb913385f00cc21b79662459a8c9fafa87/_pytest/unittest.py#L14-L22
>
> Only, instead of checking for inheritance from TestCase, I'd check for
> inheritance from our ServicePlanTestCase, and in that case I would return a
> new collector object that inherits from _pytest.python.Class, and write
> that new collector class to collect our tests. Am I barking up the right
> tree now?

pytest_pycollect_makeitem is supposed to create a single item,
while from what you describe you still have two collections nested.  You
should probably create a custom collection node for your class, which
then creates a different kind of collection node for each file which in
turn creates the actual test nodes in it's .collect().


More information about the pytest-dev mailing list