[TIP] Hint on TestClass

holger krekel holger at merlinux.eu
Wed Jul 11 23:27:32 PDT 2012


Hi Thijs,

On Wed, Jul 11, 2012 at 16:26 +0200, Thijs Engels wrote:
> Hello all,
> 
> I am currently running multiple tests (using py.test) on a series of
> objects which are relatively expensive to retrieve. My thinking was to
> group the tests on the same object into one class, potentially using the
> setup/teardown at class level to retrieve the object under test.

This is a classic use case for "funcargs".  See here for a basic example:

http://pytest.org/latest/example/parametrize.html#deferring-the-setup-of-parametrized-resources

In addition, you probably want to extend the setup of resources by
using the request.cached_setup() helper like so:

    def pytest_funcarg__db(request):
        if request.param == "d1":
            return request.cached_setup(setup=DB1, scope="session",
                                        extrakey=request.param)
        ...

This will make sure that your setup function (in this case the DB1 class)
will only be called once per session.  Other scopes are "class" or "module".
    
> As mentioned there is a series of these objects, hence I would like to
> parametrize the class holding all the individual tests. Via the search
> on the py.test I actually landed on a page which seems to describe
> exactly what I am after:
> http://pytest.org/dev/example/resources_attic.html, although I could not
> complete figure out the given example with self.db.

Not sure how you could find "attic" documents but this is not a valid
suggestion these days. 

best,
holger


> 
> This is about what I hoping to archive:
>   Use parametrize to run the test in TestSession for a list of
>   session_ids.
> 
> TestSession:
>   def __init__(self, session_id):
>     self.session = expensive_database_query(session_id)
> 
>   def test_session_id():
>     assert session.id > 0
> 
>   def test_session_date():
>     assert session.date >= date(2012, 1, 1)
> 
>   def test_periods():
>     assert periods in range(1, 26)
> 
> Is the resource_attic link indeed describing what I am after?
> 
> Thijs
> 
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python
> 



More information about the testing-in-python mailing list