tests: Use 'pytest_sessionstart'

This is the recommended way to do pre-session configuration in pytest if
not using session fixtures [1].

With this, we're able to remove the custom 'test/run.py' script in its
entirety and run 'pytest' like everyone else does. We'll do this
separately to keep things simple.

[1] https://stackoverflow.com/a/12600154/613428

Signed-off-by: Stephen Finucane <stephen@that.guru>
This commit is contained in:
Stephen Finucane 2017-12-24 20:28:46 +00:00
parent c33ecd1f8f
commit 529c96a3c9
2 changed files with 32 additions and 28 deletions

View File

@ -8,7 +8,9 @@
"""
import os
import shutil
import sys
import warnings
import pytest
from sphinx.testing.path import path
@ -31,3 +33,32 @@ def rootdir():
def pytest_report_header(config):
return 'Running Sphinx test suite (with Python %s)...' % (
sys.version.split()[0])
def _filter_warnings():
def ignore(**kwargs): warnings.filterwarnings('ignore', **kwargs)
ignore(category=DeprecationWarning, module='site') # virtualenv
ignore(category=PendingDeprecationWarning, module=r'_pytest\..*')
ignore(category=ImportWarning, module='backports')
ignore(category=ImportWarning, module='pkgutil')
ignore(category=ImportWarning, module='pytest_cov')
def _initialize_test_directory(session):
testroot = os.path.join(str(session.config.rootdir), 'tests')
tempdir = os.path.abspath(os.getenv('SPHINX_TEST_TEMPDIR',
os.path.join(testroot, 'build')))
os.environ['SPHINX_TEST_TEMPDIR'] = tempdir
print('Temporary files will be placed in %s.' % tempdir)
if os.path.exists(tempdir):
shutil.rmtree(tempdir)
os.makedirs(tempdir)
def pytest_sessionstart(session):
_filter_warnings()
_initialize_test_directory(session)

View File

@ -9,35 +9,8 @@
:copyright: Copyright 2007-2017 by the Sphinx team, see AUTHORS.
:license: BSD, see LICENSE for details.
"""
from __future__ import print_function
import os
import sys
import warnings
import traceback
import shutil
testroot = os.path.dirname(__file__) or '.'
sys.path.insert(0, os.path.abspath(os.path.join(testroot, os.path.pardir)))
import pytest
# filter warnings of test dependencies
warnings.filterwarnings('ignore', category=DeprecationWarning, module='site') # virtualenv
warnings.filterwarnings('ignore', category=ImportWarning, module='backports')
warnings.filterwarnings('ignore', category=ImportWarning, module='pkgutil')
warnings.filterwarnings('ignore', category=ImportWarning, module='pytest_cov')
warnings.filterwarnings('ignore', category=PendingDeprecationWarning, module=r'_pytest\..*')
# find a temp dir for testing and clean it up now
os.environ['SPHINX_TEST_TEMPDIR'] = \
os.path.abspath(os.path.join(testroot, 'build')) \
if 'SPHINX_TEST_TEMPDIR' not in os.environ \
else os.path.abspath(os.environ['SPHINX_TEST_TEMPDIR'])
tempdir = os.environ['SPHINX_TEST_TEMPDIR']
print('Temporary files will be placed in %s.' % tempdir)
if os.path.exists(tempdir):
shutil.rmtree(tempdir)
os.makedirs(tempdir)
import pytest # NOQA
sys.exit(pytest.main(sys.argv[1:]))