Skip to content

autotest: Fall back to relative imports in pytest discovery#30444

Closed
cclauss wants to merge 1 commit intoArduPilot:masterfrom
cclauss:autotest-relative-imports-avoid-confusing-pytest-discovery
Closed

autotest: Fall back to relative imports in pytest discovery#30444
cclauss wants to merge 1 commit intoArduPilot:masterfrom
cclauss:autotest-relative-imports-avoid-confusing-pytest-discovery

Conversation

@cclauss
Copy link
Contributor

@cclauss cclauss commented Jun 23, 2025

Tools/autotest/test_build_options.py and Tools/autotest/test_param_upgrade.py have filenames that will cause Pytest Discovery to think they should be tested, but these files do not contain Python unittests or pytest tests.

These files also import pysim and vehicle_test_suite from the same directory, but do not use relative imports, so pytest raises errors.

The proposed change is to use relative imports:

- from pysim import util
+ try:
+     from pysim import util
+ except ImportError: # Fall back to a relative import in pytest
+     from .pysim import util

Searches for these files:

How was this tested?
% uv tool run --python=3.12 --with=MAVProxy --with=numpy --with=pexpect pytest Tools/autotest

Warnings were raised instead of crashing with a confusing error.

@cclauss cclauss force-pushed the autotest-relative-imports-avoid-confusing-pytest-discovery branch from 6dad45d to 2202f2c Compare June 23, 2025 20:34
@tpwrules
Copy link
Contributor

tpwrules commented Jun 23, 2025

These are not designed for relative imports as you can see from the CI errors.

Is there a way to warn users who try to do a global discovery that the project is not designed for it? And point them to our test scripts instead? There's this implicit assumption you are making that pytest is a thing to do but I personally am not sure it's valid. ArduPilot is not a Python project. It would be nice to make the problem clearer for those who try but reworking the whole test system to be compatible might not be feasible or desirable.

@cclauss cclauss marked this pull request as draft June 23, 2025 23:33
@cclauss
Copy link
Contributor Author

cclauss commented Jun 23, 2025

When a change is proposed, we want to add tests that can be found and run automatically. What I was looking for was a GitHub Action that detects new tests based on naming conventions. If we need to pytest --ignore a lot of the codebase, then contributors will have to do a fair amount of verification to understand if their tests will be run.

@tpwrules
Copy link
Contributor

That is what the autotest framework is for: https://ardupilot.org/dev/docs/the-ardupilot-autotest-framework.html

If you are proposing e.g. testing that framework itself, which is not unreasonable, we probably need a dedicated directory for that and to always point pytest there. But currently pytest is not a thing that ArduPilot itself uses. Many sub-projects and some areas within ArduPilot (like DDS) do use it though.

For better or worse, the current tests for the build system and the autotest framework are generally just CI, i.e. the things that use them. Plus a culture to think through and manually exercise changes (e.g. test_param_upgrade.py is a script designed to help with that). There are many problems that turn up only on physical vehicles as well.

We could always use more testing but I think it needs a more principled and tailored approach than making the whole project pytestable. Perhaps, like in my previous suggestion, just making it clear to users that pytest is not the right way to interact with it to avoid creating more confusion.

@cclauss cclauss closed this Jun 24, 2025
@cclauss cclauss reopened this Jun 24, 2025
@cclauss cclauss force-pushed the autotest-relative-imports-avoid-confusing-pytest-discovery branch from 2202f2c to 326bbca Compare June 24, 2025 11:17
@cclauss cclauss changed the title autotest: Use relative imports to avoid confusing pytest discovery autotest: Fall back ti relative imports in pytest discovery Jun 24, 2025
@cclauss cclauss changed the title autotest: Fall back ti relative imports in pytest discovery autotest: Fall back to relative imports in pytest discovery Jun 24, 2025
@cclauss cclauss marked this pull request as ready for review June 24, 2025 11:20
@cclauss cclauss force-pushed the autotest-relative-imports-avoid-confusing-pytest-discovery branch from 326bbca to 13b7069 Compare June 25, 2025 10:59
@cclauss cclauss force-pushed the autotest-relative-imports-avoid-confusing-pytest-discovery branch 3 times, most recently from edfbeab to ddb3b37 Compare July 5, 2025 21:07
@cclauss
Copy link
Contributor Author

cclauss commented Jul 5, 2025

% pytest --ignore=modules/gtest \
         --ignore=modules/mavlink/pymavlink/tests/test_wp.py \
         --ignore=modules/waf \
         --ignore=Tools/autotest/test_build_options.py
         --ignore=Tools/autotest/test_param_upgrade.py
         --ignore=Tools/FilterTestTool \
         --ignore=Tools/ros2/ardupilot_dds_tests/test

============================= test session starts ==============================
platform linux -- Python 3.13.5, pytest-8.4.1, pluggy-1.6.0
rootdir: /home/runner/work/ardupilot/ardupilot
configfile: pyproject.toml
collected 160 items

Tools/autotest/unittest/annotate_params_test.py ........................ [ 15%]
                                                                         [ 15%]
Tools/autotest/unittest/extract_param_defaults_test.py ................. [ 25%]
.                                                                        [ 26%]
Tools/scripts/param_check_test.py ...........                            [ 33%]
[ ... ]

================ 153 passed, 7 skipped, 127 warnings in 30.91s =================

@cclauss cclauss force-pushed the autotest-relative-imports-avoid-confusing-pytest-discovery branch from ddb3b37 to 9ceb638 Compare July 5, 2025 22:11
Copy link
Contributor

@peterbarker peterbarker left a comment

Choose a reason for hiding this comment

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

Needs to pass CI.

@robertlong13 will need to be happy with the changes to the parameter extraction test stuff.

Comment on lines +23 to +28
try:
import vehicle_test_suite
from pysim import util
except (ImportError, ModuleNotFoundError): # Fall back to a relative import in pytest
from . import vehicle_test_suite
from .pysim import util
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm really concerned about bending our code to suit a new tool like this.

ArduPilot is not a Python project. I'm not sure we should be conforming to the conventions of one.

Can pytest be configured via a file in the root directory to ignore these files instead of modifying them here?

@cclauss cclauss force-pushed the autotest-relative-imports-avoid-confusing-pytest-discovery branch 4 times, most recently from c1a6167 to 8e9f8a0 Compare July 6, 2025 09:20
`Tools/autotest/test_build_options.py` and `Tools/autotest/test_param_upgrade.py` have filenames that will cause Pytest Discovery to think they should be tested, but these files do not contain Python `unittests` or `pytest` tests.
* https://docs.pytest.org/en/stable/explanation/goodpractices.html#conventions-for-python-test-discovery

These files also import `pysim` and `vehicle_test_suite` from the same directory, but do not use relative imports, so pytest raises errors.

The proposed change is to use relative imports:
```diff
- from pysim import util
+ try:
+     from pysim import util
+ except ImportError: # Fall back to a relative import in pytest
+     from .pysim import util
```

Searches for these files:
* https://github.com/search?q=repo%3AArduPilot%2Fardupilot%20test_build_options.py&type=code
* https://github.com/search?q=repo%3AArduPilot%2Fardupilot%20test_param_upgrade.py&type=code

How was this tested?
% `uv tool run --python=3.12 --with=MAVProxy --with=numpy --with=pexpect pytest Tools/autotest`

Warnings were raised instead of crashing with a confusing error.
@cclauss cclauss force-pushed the autotest-relative-imports-avoid-confusing-pytest-discovery branch from 8e9f8a0 to 958897a Compare July 6, 2025 10:53
@cclauss cclauss closed this Jul 6, 2025
@cclauss cclauss deleted the autotest-relative-imports-avoid-confusing-pytest-discovery branch July 6, 2025 11:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants