|
1 | 1 | from __future__ import absolute_import, division, print_function
|
2 | 2 | import pytest, py
|
3 | 3 |
|
4 |
| -from _pytest.main import Session, EXIT_NOTESTSCOLLECTED |
| 4 | +from _pytest.main import Session, EXIT_NOTESTSCOLLECTED, _in_venv |
5 | 5 |
|
6 | 6 | class TestCollector(object):
|
7 | 7 | def test_collect_versus_item(self):
|
@@ -121,6 +121,53 @@ def test_ignored_certain_directories(self, testdir):
|
121 | 121 | assert "test_notfound" not in s
|
122 | 122 | assert "test_found" in s
|
123 | 123 |
|
| 124 | + @pytest.mark.parametrize('fname', |
| 125 | + ("activate", "activate.csh", "activate.fish", |
| 126 | + "Activate", "Activate.bat", "Activate.ps1")) |
| 127 | + def test_ignored_virtualenvs(self, testdir, fname): |
| 128 | + bindir = "Scripts" if py.std.sys.platform.startswith("win") else "bin" |
| 129 | + testdir.tmpdir.ensure("virtual", bindir, fname) |
| 130 | + testfile = testdir.tmpdir.ensure("virtual", "test_invenv.py") |
| 131 | + testfile.write("def test_hello(): pass") |
| 132 | + |
| 133 | + # by default, ignore tests inside a virtualenv |
| 134 | + result = testdir.runpytest() |
| 135 | + assert "test_invenv" not in result.stdout.str() |
| 136 | + # allow test collection if user insists |
| 137 | + result = testdir.runpytest("--collect-in-virtualenv") |
| 138 | + assert "test_invenv" in result.stdout.str() |
| 139 | + # allow test collection if user directly passes in the directory |
| 140 | + result = testdir.runpytest("virtual") |
| 141 | + assert "test_invenv" in result.stdout.str() |
| 142 | + |
| 143 | + @pytest.mark.parametrize('fname', |
| 144 | + ("activate", "activate.csh", "activate.fish", |
| 145 | + "Activate", "Activate.bat", "Activate.ps1")) |
| 146 | + def test_ignored_virtualenvs_norecursedirs_precedence(self, testdir, fname): |
| 147 | + bindir = "Scripts" if py.std.sys.platform.startswith("win") else "bin" |
| 148 | + # norecursedirs takes priority |
| 149 | + testdir.tmpdir.ensure(".virtual", bindir, fname) |
| 150 | + testfile = testdir.tmpdir.ensure(".virtual", "test_invenv.py") |
| 151 | + testfile.write("def test_hello(): pass") |
| 152 | + result = testdir.runpytest("--collect-in-virtualenv") |
| 153 | + assert "test_invenv" not in result.stdout.str() |
| 154 | + # ...unless the virtualenv is explicitly given on the CLI |
| 155 | + result = testdir.runpytest("--collect-in-virtualenv", ".virtual") |
| 156 | + assert "test_invenv" in result.stdout.str() |
| 157 | + |
| 158 | + @pytest.mark.parametrize('fname', |
| 159 | + ("activate", "activate.csh", "activate.fish", |
| 160 | + "Activate", "Activate.bat", "Activate.ps1")) |
| 161 | + def test__in_venv(self, testdir, fname): |
| 162 | + """Directly test the virtual env detection function""" |
| 163 | + bindir = "Scripts" if py.std.sys.platform.startswith("win") else "bin" |
| 164 | + # no bin/activate, not a virtualenv |
| 165 | + base_path = testdir.tmpdir.mkdir('venv') |
| 166 | + assert _in_venv(base_path) is False |
| 167 | + # with bin/activate, totally a virtualenv |
| 168 | + base_path.ensure(bindir, fname) |
| 169 | + assert _in_venv(base_path) is True |
| 170 | + |
124 | 171 | def test_custom_norecursedirs(self, testdir):
|
125 | 172 | testdir.makeini("""
|
126 | 173 | [pytest]
|
|
0 commit comments