Repository: pytest-dev/pytest-xdist (python)

Issue:
Option to set the default distribution algorithm
Hi, I have the following scenario:

- I have some tests that need to be run in the same worker due to the fact they use a shared object (I know it's not very idiomatic for unit tests, but it is really the simpler way to check what I need to check)
- I saw the `--dist loadgroup` option, but unfortunately it is not the default, so I should remember to specify every time the command line option
- I can't add it to the `addopts` setting since it errors if I don't specify either `-n` or `-tx`, and I want to be able to choose to run the tests the standard way too.

What could work for me is a setting to set the default algorithm to use when `--dist` is not specified, so that:
- if I run `pytest` standalone, it doesn't run in parallel
- if I run `pytest -n auto`, it uses the algorithm specified in the setting
- if I run `pytest -n auto --dist loadfile`, it overrides the setting with the `loadfile` algorithm.

Pull request:
Let users configure dist mode in the configuration file
Fix #789



Patch:
diff --git a/changelog/789.feature.rst b/changelog/789.feature.rst
new file mode 100644
index 0000000..426583c
--- /dev/null
+++ b/changelog/789.feature.rst
@@ -0,0 +1,6 @@
+Users can now set a default distribution mode in their configuration file:
+
+.. code-block:: ini
+
+    [pytest]
+    addopts = --dist loadscope
diff --git a/src/xdist/plugin.py b/src/xdist/plugin.py
index a24864e..8c22f9b 100644
--- a/src/xdist/plugin.py
+++ b/src/xdist/plugin.py
@@ -190,7 +190,19 @@ def pytest_addhooks(pluginmanager):
 
 @pytest.hookimpl(trylast=True)
 def pytest_configure(config):
-    if config.getoption("dist") != "no" and not config.getvalue("collectonly"):
+    config_line = (
+        "xdist_group: specify group for tests should run in same session."
+        "in relation to one another. Provided by pytest-xdist."
+    )
+    config.addinivalue_line("markers", config_line)
+
+    # Skip this plugin entirely when only doing collection.
+    if config.getvalue("collectonly"):
+        return
+
+    # Create the distributed session in case we have a valid distribution
+    # mode and test environments.
+    if config.getoption("dist") != "no" and config.getoption("tx"):
         from xdist.dsession import DSession
 
         session = DSession(config)
@@ -199,6 +211,7 @@ def pytest_configure(config):
         if tr:
             tr.showfspath = False
 
+    # Deprecation warnings for deprecated command-line/configuration options.
     if config.getoption("looponfail", None) or config.getini("looponfailroots"):
         warning = DeprecationWarning(
             "The --looponfail command line argument and looponfailroots config variable are deprecated.\n"
@@ -213,12 +226,6 @@ def pytest_configure(config):
         )
         config.issue_config_time_warning(warning, 2)
 
-    config_line = (
-        "xdist_group: specify group for tests should run in same session."
-        "in relation to one another. " + "Provided by pytest-xdist."
-    )
-    config.addinivalue_line("markers", config_line)
-
 
 @pytest.hookimpl(tryfirst=True)
 def pytest_cmdline_main(config):


Test patch:
diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py
index 792852d..1666ba0 100644
--- a/testing/acceptance_test.py
+++ b/testing/acceptance_test.py
@@ -1561,3 +1561,21 @@ def test_collection_crash(testdir):
             "*= 1 error in *",
         ]
     )
+
+
+def test_dist_in_addopts(testdir):
+    """Users can set a default distribution in the configuration file (#789)."""
+    testdir.makepyfile(
+        """
+        def test():
+            pass
+        """
+    )
+    testdir.makeini(
+        """
+        [pytest]
+        addopts = --dist loadscope
+        """
+    )
+    result = testdir.runpytest()
+    assert result.ret == 0