Skip to content

Commit

Permalink
Use parameterized to enable parallelisation of slow tests
Browse files Browse the repository at this point in the history
We can't use pytest's parameterisation here as this test is a
unittest-style test, to which pytest does not support applying
parameterisation. Instead we can use the separate parameterized
package which offers parameterisation on plain unittest tests,
including when run under pytest.

Happily this also plays nicely with pytest-xdist. As the resulting
test functions are actually separate functions on the class, xdist
is able to then spread the work between workers.
  • Loading branch information
PeterJCLaw committed Oct 26, 2023
1 parent 5845828 commit b40ed6d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ test =
astroid >=2, <4; python_version >= "3"
pytest
pytest-xdist
parameterized

[options.package_data]
asttokens = py.typed
Expand Down
92 changes: 45 additions & 47 deletions tests/test_mark_tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import inspect
import io
import os
from parameterized import parameterized
import re
import sys
import textwrap
import token
from typing import List
import unittest
from time import time

import astroid
import six
Expand Down Expand Up @@ -630,7 +629,15 @@ def test_complex_slice_and_parens(self):
self.create_mark_checker(source)

if six.PY3:
def test_sys_modules(self):
def _parameterize_sys_modules():
modules = list(sys.modules.keys())
if not os.environ.get('ASTTOKENS_SLOW_TESTS'):
modules = modules[:20]

return parameterized.expand([(x,) for x in modules])

@_parameterize_sys_modules()
def test_sys_modules(self, module_name):
"""
Verify all nodes on source files obtained from sys.modules.
This can take a long time as there are many modules,
Expand All @@ -639,51 +646,42 @@ def test_sys_modules(self):
"""
from .test_astroid import AstroidTreeException

modules = list(sys.modules.values())
if not os.environ.get('ASTTOKENS_SLOW_TESTS'):
modules = modules[:20]
module = sys.modules[module_name]

try:
filename = inspect.getsourcefile(module)
except Exception: # some modules raise weird errors
self.skipTest("Failed to get source file")

if not filename:
self.skipTest("No source file for module")

filename = os.path.abspath(filename)
print(filename)
try:
with io.open(filename) as f:
source = f.read()
except OSError:
self.skipTest("Error reading source file")

if self.is_astroid_test and (
# Astroid fails with a syntax error if a type comment is on its own line
re.search(r'^\s*# type: ', source, re.MULTILINE)
):
self.skipTest("Skipping potential Astroid syntax error")

try:
self.create_mark_checker(source)
except AstroidTreeException:
# Astroid sometimes fails with errors like:
# AttributeError: 'TreeRebuilder' object has no attribute 'visit_typealias'
# See https://github.com/gristlabs/asttokens/actions/runs/6015907789/job/16318767911?pr=110
# Should be fixed in the next astroid release:
# https://github.com/pylint-dev/pylint/issues/8782#issuecomment-1669967220
# Note that this exception is raised before asttokens is even involved,
# it's purely an astroid bug that we can safely ignore.
self.skipTest("Skipping Astroid error")

start = time()
for module in modules:
# Don't let this test (which runs twice) take longer than 13 minutes
# to avoid the travis build time limit of 30 minutes
if time() - start > 13 * 60:
break

try:
filename = inspect.getsourcefile(module)
except Exception: # some modules raise weird errors
continue

if not filename:
continue

filename = os.path.abspath(filename)
print(filename)
try:
with io.open(filename) as f:
source = f.read()
except OSError:
continue

if self.is_astroid_test and (
# Astroid fails with a syntax error if a type comment is on its own line
re.search(r'^\s*# type: ', source, re.MULTILINE)
):
print('Skipping', filename)
continue

try:
self.create_mark_checker(source)
except AstroidTreeException:
# Astroid sometimes fails with errors like:
# AttributeError: 'TreeRebuilder' object has no attribute 'visit_typealias'
# See https://github.com/gristlabs/asttokens/actions/runs/6015907789/job/16318767911?pr=110
# Should be fixed in the next astroid release:
# https://github.com/pylint-dev/pylint/issues/8782#issuecomment-1669967220
# Note that this exception is raised before asttokens is even involved,
# it's purely an astroid bug that we can safely ignore.
continue

if six.PY3:
def test_dict_merge(self):
Expand Down

0 comments on commit b40ed6d

Please sign in to comment.