Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pytest-xdist to isolate tests #236

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions pyfrc/mains/cli_add_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,4 @@ def run(self, main_file: pathlib.Path, project_path: pathlib.Path):
fp.write(builtin_tests)
print("- builtin tests created at", builtin_tests_file)

print()
print("Robot tests can be ran via 'python3 -m robotpy test'")
print("Robot tests can be ran via 'robotpy test'")
43 changes: 42 additions & 1 deletion pyfrc/mains/cli_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import io
import re
import os
from os.path import abspath
import inspect
Expand All @@ -17,9 +19,44 @@
# could be a useful thing. Will have to consider that later.


import sys
import pathlib
from pyfrc.test_support.pytest_plugin import PyFrcPlugin
# Tests are always run from the top directory of the robot project
# so the location of robot.py should be the current working directory
sys.path.append('.')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I hadn't noticed that you updated the PR.

This is a bad idea because cli_test can be imported at other times via the robotpy CLI. It would be better to get the robot path from _run_test and pass the configuration to the pytest plugin. Does pytest-xdist not have a way to propagate configuration to workers?

Additionally, the robot class doesn't have to be called MyRobot, see the logic at https://github.com/robotpy/robotpy-cli/blob/main/robotpy/main.py#L28

An alternative solution would be to set an environment variable and use that to get the robot path and class name.

import robot

def pytest_configure(config):
if config.pluginmanager.has_plugin("pyfrc_plugin"):
# Avoid double registration
return
robot_class = robot.MyRobot
robot_file = './robot.py'
plugin = PyFrcPlugin(robot_class, robot_file)
config.pluginmanager.register(plugin, "pyfrc_plugin")


class _TryAgain(Exception):
pass

def count_tests(test_path='.'):
import subprocess
# Run pytest in collect-only mode to get test count
result = subprocess.run(
['pytest', '--collect-only', '-v', test_path],
capture_output=True,
text=True
)
print('subprocess result: ')
print(result.stdout)

# Count lines that look like test collections
test_lines = [line for line in result.stdout.split('\n')
if re.search(r'\s*test_\w+\[?', line) and 'cachedir' not in line]

test_count = len(test_lines)
return test_count

#
# main test class
Expand Down Expand Up @@ -112,8 +149,12 @@ def _run_test(
pytest_args.insert(0, abspath(inspect.getfile(basic)))

try:
test_count = count_tests()
print(f'Running {test_count} parallel workers')

args = ['-v', '-n', str(test_count)] + pytest_args
retv = pytest.main(
pytest_args,
args,
plugins=[pytest_plugin.PyFrcPlugin(robot_class, main_file)],
)
finally:
Expand Down
2 changes: 2 additions & 0 deletions pyfrc/test_support/pytest_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def __init__(self, robot_class: Type[wpilib.RobotBase], robot_file: pathlib.Path
robot_file.parent,
)

print('robot file: ', robot_file)

# Tests need to know when robotInit is called, so override the robot
# to do that
class TestRobot(robot_class):
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ packages = find:
install_requires =
pytest>=3.9
pytest-reraise
pytest-xdist>=3.6.1
pint>=0.11.0

wpilib>=2025.0.0b1,<2026
Expand Down
Loading