I would like to track the functions invoked by pytest test cases. I use this test file in xarray as an example. I use the following command to trace
python -m trace --trackcalls --module pytest -rA xarray/tests/test_coding.py -n 64
In the output I am able to see test cases are successfully executed.
============================= test session starts ==============================
platform linux -- Python 3.10.16, pytest-8.3.4, pluggy-1.5.0
rootdir: /.../xarray
configfile: pyproject.toml
plugins: timeout-2.3.1, cov-6.0.0, metadata-3.1.1, env-1.1.5, json-0.4.0, hypothesis-6.125.1, xdist-3.6.1, json-report-1.5.0
created: 64/64 workers
64 workers [31 items]
............................... [100%]
==================================== PASSES ====================================
=========================== short test summary info ============================
PASSED xarray/tests/test_coding.py::test_coder_roundtrip
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f4-i1]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f4-u2]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f4-u1]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f8-f2]
PASSED xarray/tests/test_coding.py::test_CFMaskCoder_encode_missing_fill_values_conflict[numeric-without-dtype]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f4-f2]
PASSED xarray/tests/test_coding.py::test_CFMaskCoder_missing_value
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f4-f4]
PASSED xarray/tests/test_coding.py::test_scaling_offset_as_list[0.1-scale_factor1]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f8-u1]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f8-f4]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f8-i1]
PASSED xarray/tests/test_coding.py::test_CFMaskCoder_decode
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f8-i2]
PASSED xarray/tests/test_coding.py::test_CFMaskCoder_encode_missing_fill_values_conflict[numeric-with-dtype]
PASSED xarray/tests/test_coding.py::test_scaling_offset_as_list[0.1-10]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f8-u2]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f4-i2]
PASSED xarray/tests/test_coding.py::test_CFMaskCoder_encode_missing_fill_values_conflict[times-with-dtype]
PASSED xarray/tests/test_coding.py::test_scaling_offset_as_list[add_offset1-10]
PASSED xarray/tests/test_coding.py::test_scaling_offset_as_list[add_offset1-scale_factor1]
PASSED xarray/tests/test_coding.py::test_decode_unsigned_from_signed[1]
PASSED xarray/tests/test_coding.py::test_decode_unsigned_from_signed[2]
PASSED xarray/tests/test_coding.py::test_decode_signed_from_unsigned[2]
PASSED xarray/tests/test_coding.py::test_decode_unsigned_from_signed[4]
PASSED xarray/tests/test_coding.py::test_decode_unsigned_from_signed[8]
PASSED xarray/tests/test_coding.py::test_decode_signed_from_unsigned[1]
PASSED xarray/tests/test_coding.py::test_decode_signed_from_unsigned[8]
PASSED xarray/tests/test_coding.py::test_decode_signed_from_unsigned[4]
PASSED xarray/tests/test_coding.py::test_CFMaskCoder_decode_dask
======================== 31 passed in 113.20s (0:01:53) ========================
calling relationships:
...
But in the following calling relationships, none of the test methods and the methods that are supposed to be invoked are included. I would like to know why and how to resolve this issue. Thanks.
I would like to track the functions invoked by pytest test cases. I use this test file in xarray as an example. I use the following command to trace
python -m trace --trackcalls --module pytest -rA xarray/tests/test_coding.py -n 64
In the output I am able to see test cases are successfully executed.
============================= test session starts ==============================
platform linux -- Python 3.10.16, pytest-8.3.4, pluggy-1.5.0
rootdir: /.../xarray
configfile: pyproject.toml
plugins: timeout-2.3.1, cov-6.0.0, metadata-3.1.1, env-1.1.5, json-0.4.0, hypothesis-6.125.1, xdist-3.6.1, json-report-1.5.0
created: 64/64 workers
64 workers [31 items]
............................... [100%]
==================================== PASSES ====================================
=========================== short test summary info ============================
PASSED xarray/tests/test_coding.py::test_coder_roundtrip
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f4-i1]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f4-u2]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f4-u1]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f8-f2]
PASSED xarray/tests/test_coding.py::test_CFMaskCoder_encode_missing_fill_values_conflict[numeric-without-dtype]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f4-f2]
PASSED xarray/tests/test_coding.py::test_CFMaskCoder_missing_value
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f4-f4]
PASSED xarray/tests/test_coding.py::test_scaling_offset_as_list[0.1-scale_factor1]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f8-u1]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f8-f4]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f8-i1]
PASSED xarray/tests/test_coding.py::test_CFMaskCoder_decode
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f8-i2]
PASSED xarray/tests/test_coding.py::test_CFMaskCoder_encode_missing_fill_values_conflict[numeric-with-dtype]
PASSED xarray/tests/test_coding.py::test_scaling_offset_as_list[0.1-10]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f8-u2]
PASSED xarray/tests/test_coding.py::test_scaling_converts_to_float[f4-i2]
PASSED xarray/tests/test_coding.py::test_CFMaskCoder_encode_missing_fill_values_conflict[times-with-dtype]
PASSED xarray/tests/test_coding.py::test_scaling_offset_as_list[add_offset1-10]
PASSED xarray/tests/test_coding.py::test_scaling_offset_as_list[add_offset1-scale_factor1]
PASSED xarray/tests/test_coding.py::test_decode_unsigned_from_signed[1]
PASSED xarray/tests/test_coding.py::test_decode_unsigned_from_signed[2]
PASSED xarray/tests/test_coding.py::test_decode_signed_from_unsigned[2]
PASSED xarray/tests/test_coding.py::test_decode_unsigned_from_signed[4]
PASSED xarray/tests/test_coding.py::test_decode_unsigned_from_signed[8]
PASSED xarray/tests/test_coding.py::test_decode_signed_from_unsigned[1]
PASSED xarray/tests/test_coding.py::test_decode_signed_from_unsigned[8]
PASSED xarray/tests/test_coding.py::test_decode_signed_from_unsigned[4]
PASSED xarray/tests/test_coding.py::test_CFMaskCoder_decode_dask
======================== 31 passed in 113.20s (0:01:53) ========================
calling relationships:
...
But in the following calling relationships, none of the test methods and the methods that are supposed to be invoked are included. I would like to know why and how to resolve this issue. Thanks.
Share Improve this question asked 2 days ago Richard HuRichard Hu 8917 silver badges22 bronze badges1 Answer
Reset to default 0pytest sometimes runs tests in subprocesses (especially with certain plugins like pytest-xdist for parallel test execution). The trace module typically doesn't capture calls in subprocesses because it's designed to trace the main process.
Solution: Run pytest without parallelism (pytest -n 1), so the tests run in the main process.