pytest-xdist makes the suite faster and the flakes weirder
Blog post from Mergify
The text delves into the challenges and solutions associated with flaky tests in pytest, particularly when using pytest-xdist to distribute tests across multiple workers. It describes a common issue where tests that pass individually fail when run in parallel due to shared external states, such as file paths in temporary directories, which lead to race conditions. The text suggests solutions like using pytest's tmp_path fixture to give each test its own directory or employing tmp_path_factory at session scope for genuinely shared resources, ensuring that no race conditions occur. Additionally, it highlights the importance of choosing the right scheduler, such as loadscope, loadfile, or worksteal, based on the test suite's characteristics to minimize cross-test interference. The document also explains how tools like Mergify Test Insights can help identify and quarantine these race conditions by analyzing failures based on xdist worker IDs, thus preventing them from reaching production unnoticed.