Skip to content

Instantly share code, notes, and snippets.

@tgs
Created April 1, 2015 21:00
Show Gist options
  • Save tgs/ceae4da559ec9f27a177 to your computer and use it in GitHub Desktop.
Save tgs/ceae4da559ec9f27a177 to your computer and use it in GitHub Desktop.
py.test: ignore the same files as git when doing test collection
import subprocess
# Usage: put it in the current directory, and run
# py.test -p gitignore [...]
def pytest_ignore_collect(path, config):
if path.basename == '.git': # Ignore .git directory
return True
child = subprocess.Popen(['git', 'check-ignore', str(path)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
output = child.communicate()[0]
status = child.wait()
# Possible return values: (via git help check-ignore)
# 0: Yes, the file is ignored
# 1: No, the file isn't ignored
# 128: Fatal error, git can't tell us whether to ignore
#
# The latter happens a lot with python virtualenvs, since they have
# symlinks and git gives up when you try to follow one. But maybe you have
# a test directory that you include with a symlink, who knows?
return status == 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment