1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
import pytest
import numpy
NUMPY_VERSION = numpy.__version__
pytest_plugins = ['pytester']
def extract_package_version_lines(output):
lines = []
in_section = False
for line in output.splitlines():
if line.strip() == 'Package versions:':
in_section = True
elif in_section:
if line.strip() == "":
break
else:
lines.append(line)
return lines
def test_default(testdir, capsys):
testdir.inline_run()
out, err = capsys.readouterr()
assert 'Package versions:' not in out
@pytest.mark.parametrize('method', ['cli', 'ini', 'conftest'])
def test_enabled(testdir, capsys, method):
if method == 'cli':
testdir.inline_run("--astropy-header")
elif method == 'ini':
testdir.makeini("""
[pytest]
astropy_header = yes
""")
testdir.inline_run()
elif method == 'conftest':
testdir.makeconftest("""
def pytest_configure(config):
config.option.astropy_header = True
""")
testdir.inline_run()
out, err = capsys.readouterr()
lines = extract_package_version_lines(out)
assert len(lines) == 5
assert lines[0].startswith('Numpy: ')
assert lines[1].startswith('Scipy: ')
assert lines[2].startswith('Matplotlib: ')
assert lines[3].startswith('h5py: ')
assert lines[4].startswith('Pandas: ')
@pytest.mark.parametrize('method', ['ini', 'conftest'])
def test_explicit_disable(testdir, capsys, method):
if method == 'ini':
testdir.makeini("""
[pytest]
astropy_header = no
""")
testdir.inline_run()
elif method == 'conftest':
testdir.makeconftest("""
def pytest_configure(config):
config.option.astropy_header = False
""")
testdir.inline_run()
out, err = capsys.readouterr()
assert 'Package versions:' not in out
@pytest.mark.parametrize('method', ['cli', 'ini', 'ini_list', 'conftest'])
def test_override_package_single(testdir, capsys, method):
if method == 'cli':
testdir.inline_run("--astropy-header", "--astropy-header-packages=numpy")
elif method == 'ini':
testdir.makeini("""
[pytest]
astropy_header = yes
astropy_header_packages = numpy
""")
testdir.inline_run()
elif method == 'ini_list':
testdir.makeini("""
[pytest]
astropy_header = yes
astropy_header_packages =
numpy
""")
testdir.inline_run()
elif method == 'conftest':
testdir.makeconftest("""
def pytest_configure(config):
config.option.astropy_header = True
config.option.astropy_header_packages = ['numpy']
""")
testdir.inline_run()
out, err = capsys.readouterr()
lines = extract_package_version_lines(out)
assert len(lines) == 1
assert lines[0] == f'numpy: {NUMPY_VERSION}'
@pytest.mark.parametrize('method', ['cli', 'ini', 'ini_list', 'conftest'])
def test_override_package_multiple(testdir, capsys, method):
if method == 'cli':
testdir.inline_run("--astropy-header", "--astropy-header-packages=numpy,pandas")
elif method == 'ini':
testdir.makeini("""
[pytest]
astropy_header = yes
astropy_header_packages = numpy, pandas
""")
testdir.inline_run()
elif method == 'ini_list':
testdir.makeini("""
[pytest]
astropy_header = yes
astropy_header_packages =
numpy
pandas
""")
testdir.inline_run()
elif method == 'conftest':
testdir.makeconftest("""
def pytest_configure(config):
config.option.astropy_header = True
config.option.astropy_header_packages = ['numpy', 'pandas']
""")
testdir.inline_run()
out, err = capsys.readouterr()
print(out)
lines = extract_package_version_lines(out)
assert len(lines) == 2
assert lines[0] == f'numpy: {NUMPY_VERSION}'
assert lines[1].startswith('pandas')
@pytest.mark.parametrize('method', ['cli', 'ini', 'ini_list', 'conftest'])
def test_nonexistent(testdir, capsys, method):
if method == 'cli':
testdir.inline_run("--astropy-header", "--astropy-header-packages=apackagethatdoesnotexist")
elif method == 'ini':
testdir.makeini("""
[pytest]
astropy_header = yes
astropy_header_packages = apackagethatdoesnotexist
""")
testdir.inline_run()
elif method == 'ini_list':
testdir.makeini("""
[pytest]
astropy_header = yes
astropy_header_packages =
apackagethatdoesnotexist
""")
testdir.inline_run()
elif method == 'conftest':
testdir.makeconftest("""
def pytest_configure(config):
config.option.astropy_header = True
config.option.astropy_header_packages = ['apackagethatdoesnotexist']
""")
testdir.inline_run()
out, err = capsys.readouterr()
lines = extract_package_version_lines(out)
assert len(lines) == 1
assert lines[0] == 'apackagethatdoesnotexist: not available'
def test_modify_in_conftest(testdir, capsys):
testdir.makeconftest("""
from pytest_astropy_header.display import PYTEST_HEADER_MODULES, TESTED_VERSIONS
def pytest_configure(config):
config.option.astropy_header = True
PYTEST_HEADER_MODULES.pop('Pandas')
PYTEST_HEADER_MODULES['scikit-image'] = 'skimage'
TESTED_VERSIONS['fakepackage'] = '1.0.2'
""")
testdir.inline_run()
out, err = capsys.readouterr()
assert err == ''
lines = extract_package_version_lines(out)
assert len(lines) == 5
assert lines[0].startswith('Numpy: ')
assert lines[1].startswith('Scipy: ')
assert lines[2].startswith('Matplotlib: ')
assert lines[3].startswith('h5py: ')
assert lines[4].startswith('scikit-image: ')
assert 'Running tests with fakepackage version 1.0.2' in out
|