[py-dev] py.test api - Running tests in a module object?

holger krekel holger at merlinux.eu
Mon Nov 29 16:20:05 CET 2010


On Thu, Aug 05, 2010 at 14:39 -0700, Sridhar Ratnakumar wrote:
> 
> On 2010-08-05, at 2:28 PM, holger krekel wrote:
> 
> > On Thu, Aug 05, 2010 at 14:24 -0700, Sridhar Ratnakumar wrote:
> >> On 8/5/2010 2:06 PM, holger krekel wrote:
> >>> On Thu, Aug 05, 2010 at 13:46 -0700, Sridhar Ratnakumar wrote:
> >>>>> I am trying to run py.test on a given module object dynamically.
> >>>>> 
> >>>>>       mod = __import__('company.foo.somemod')
> >>>>>       py.test.run(mod)
> >>>>> 
> >>>>> A quick glance in the py.test source doesn't seem like a trivial thing.
> >>>>> IPython introspection did not give me any clue either.
> >>>>> 
> >>>>> Is there a way to do this at all? I did try
> >>>>> py.test.cmdline.main(['...']), but that accepts only file path, not the
> >>>>> Python module object itself.
> >>> there currently is no direct support for running tests in python modules.
> >>> py.test basically always starts from the file system. Does it help you
> >>> to try to fish the file from somemod.__file__ and pass this to cmdline.main()?
> >>> You only expect it to collect tests of a single module, right?
> >>> I guess we could add some more direct support for this if
> >>> you continue to have the need (please create an issue if so).
> >> 
> >> Hmm, __file__ is a workaround, but it seems to work fine for my use  
> >> case. I was just wondering if an API was exposed.
> >> 
> >> I am happy with __file__ for now.
> > 
> > What do you think about automating this lookup and allowing
> > 
> >    py.test.cmdline.main([module])
> > 
> > maybe that's enough? 
> 
> 
> That might have helped, but then - just now, I rewrote some routines in my code to simply rely on .py file paths instead of modules. This simplified the code a bit. So now I have come to prefer file paths. :-) If I ever have the need for running tests on module/class/collection-of-funcs objects, I will be sure to open a feature request. Thanks.

FYI py.test-2.0 now supports the "--pyargs" option:

    py.test --pyargs company.foo.somemod

so that this will discover the __file__'s directory and collect tests there.
You can make this permanent, see here:
http://pytest.org/customize.html#adding-default-options

cheers,
holger



More information about the Pytest-dev mailing list