13

I am trying to run py.test on my package but it is trying to parse setup.py from the project root directory even if I tried to exclude it.

I need to collect the tests from *.py files because the test classes are included in the modules.

# setup.cfg
[pytest]
norecursedirs = .svn _build tmp* lib/third lib *.egg bin distutils setup.py
python_files = *.py

Still when I run py.test it will give me ERROR collecting setup.py which I already excluded.

/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/distutils/core.py:140: in setup
>           raise SystemExit, gen_usage(dist.script_name) + "\nerror: %s" % msg
E           SystemExit: usage: py.test [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
E              or: py.test --help [cmd1 cmd2 ...]
E              or: py.test --help-commands
E              or: py.test cmd --help
E           
E           error: no commands supplied
sorin
  • 161,544
  • 178
  • 535
  • 806

4 Answers4

20

You can configure the --ignore option to your pytest.ini configuration like this maybe:

addopts = --ignore=setup.py

which should help if you are in the root directory and want py.test to ignore the setup.py file.

hpk42
  • 21,501
  • 4
  • 47
  • 53
  • Damn, it means that if I have another `setup.py` it will also be included. Also, tox is not able to look for tox.ini in the parent directories?! – sorin May 29 '12 at 23:49
  • 1
    To avoid other setup.py files being included i think pytest.ini would need a way to allow specifying exact locations like --ignore={inidir}/setup.py or so. – hpk42 May 30 '12 at 15:43
2

From the docs (and the name of the variable) it looks like norecursedirs only skips directories, so since setup.py is a file, putting it in norecursedirs has no effect.

http://pytest.org/latest/customize.html?highlight=norecursedirs#confval-norecursedirs

glyphobet
  • 1,564
  • 11
  • 17
  • Thanks, for remarking it. Still this doesn't solve my problem. From these I would assume that it's impossible to prevent `py.test` from loading `setup.py` if you want to look for `*.py`. Still the error appears because `py.test` is trying to execute `setup.py`. Maybe there is something I can do with setup.py to make it `py.test` friendly. – sorin May 29 '12 at 20:12
1

The pytest docs have the answer buried away here. Put a conftest.py file in the root directory of your project that lists the files you want pytest to ignore:

However, many projects will have a setup.py which they don’t want to be imported. Moreover, there may files only importable by a specific python version. For such cases you can dynamically define files to be ignored by listing them in a conftest.py file:


 # content of conftest.py
 import sys

 collect_ignore = ["setup.py"]

Will
  • 4,241
  • 4
  • 39
  • 48
0

In Jedi we've solved this by adding a conftest.py file with the content of collect_ignore = ["setup.py"]. That makes it "magically" work.

IMHO py.test should make it easier to ignore setup.py, because that's a very typical use case.

Dave Halter
  • 15,556
  • 13
  • 76
  • 103