[py-dev] tests running order

holger krekel hpk at trillke.net
Thu Jun 9 14:00:19 CEST 2005


Hi Philippe, 

On Thu, Jun 09, 2005 at 13:28 +0200, Philippe Normand wrote:
> I'd like my unit-tests to run in a specific order. For instance take two
> files test_foo.py and test_bar.py. I suppose if i run py.test, test_bar
> tests will be executed before test_foo (alphabetic order). Now if i want
> to run test_foo before test_bar ?
> 
> Well the following catch may work:
> 
> py.test test_foo.py
> py.test test_bar.py
> 
> But in a more complex setup (many files), this can become quite
> painful.. Any ideas ?

You will need to put a conftest.py file into your testing directory
with the following example content: 

    import py
    mydir = py.magic.autopath().dirpath()  # directory of this conftest.py 

    class Directory(py.test.collect.Directory):
        def run(self):
            if self.fspath != mydir:  # somewhere below 
                return super(Directory, self).run()
            return ['test_foo.py', 'test_bar.py']


conftest.py files are reponsible for their complete subdirectory
hierarchy, therefore we need to make sure we are realling 
being asked (via the session calling our run() method) 
for our own exact directory.  

run() methods need to return a list of basenames.  If you
take over "test collection" like this then you need to explicitely 
list all files as py.test will obviously not collect files 
automatically in this directory anymore.  

makes all sense to you?  You may want to read this to understand more: 

    http://codespeak.net/py/current/doc/test.html#collecting-and-running-tests-implementation-remarks

(sorry for the long URL). 

cheers, 

    holger


More information about the py-dev mailing list