Repository: pytest-dev/pytest-cov (language: python)

Issue:
pytest --collect-only fails when 'fail under' is set on a configuration
# Summary
My pyproject.toml have the following sections (among other):
```toml
[tool.pytest.ini_options]
minversion = '6.0'
addopts = '--quiet --cov'

[tool.coverage.run]
source = ['my_module']

[tool.coverage.report]
fail_under = 75
```
 
And some tools (such as VSCode Python's plugin) depends on `pytest --collect-only`. However, when running that it fails as it doesn't actually run the tests and it displays a coverage way below the real value.
 
## Expected vs actual result
Expected:
when running `pytest --collect-only` I'd expect it only to fail if for some reason it collect the list of tests, ignoring the fail under parameter.
 
Actual result:
`pytest --collect-only` fails stating the coverage didn't reach the goal.
 
# Reproducer
```
git clone https://github.com/maurofaccenda/pytest-cov-issue.git
cd pytest-cov-issue/
python3 -m venv venv
source ./venv/bin/activate
pip install pip --upgrade
pip install -r requirements.txt
pytest  # succeeds
pytest --collect-only  # fails
echo $?  # prints 1
```
 
## Versions

`pip list` output:
```
Package        Version
-------------- -------
attrs          20.1.0
coverage       5.2.1
iniconfig      1.0.1
more-itertools 8.4.0
packaging      20.4
pip            20.2.2
pkg-resources  0.0.0
pluggy         0.13.1
py             1.9.0
pyparsing      2.4.7
pytest         6.0.1
pytest-cov     2.10.1
setuptools     44.0.0
six            1.15.0
toml           0.10.1
```

```
$ python --version
Python 3.8.2
```

```
$ pytest --version
pytest 6.0.1
```

## Config
```toml
[tool.pytest.ini_options]
minversion = '6.0'
addopts = '--quiet --cov'

[tool.coverage.run]
source = ['my_module']

[tool.coverage.report]
fail_under = 75
```

## Code

https://github.com/maurofaccenda/pytest-cov-issue


Discussion / hints:
Maybe this is related to `pytest` alone, but as it wouldn't happen without `pytest-cov`, I thought it would make sense to create the issue here. 

Fix patch:
diff --git a/AUTHORS.rst b/AUTHORS.rst
index 6aa916a1..489903c2 100644
--- a/AUTHORS.rst
+++ b/AUTHORS.rst
@@ -50,3 +50,4 @@ Authors
 * Brian Rutledge - https://github.com/bhrutledge
 * Danilo Šegan - https://github.com/dsegan
 * Michał Bielawski - https://github.com/D3X
+* Zac Hatfield-Dodds - https://github.com/Zac-HD
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index e6e47d95..684720ff 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -2,6 +2,14 @@ Changelog
 =========
 
 
+3.1.0 (future)
+-------------------
+
+* `--cov-fail-under` no longer causes `pytest --collect-only` to fail
+  Contributed by Zac Hatfield-Dodds in
+  `#511 <https://github.com/pytest-dev/pytest-cov/pull/511>`_.
+
+
 3.0.0 (2021-10-04)
 -------------------
 


Test patch:
diff --git a/src/pytest_cov/plugin.py b/src/pytest_cov/plugin.py
index 94a1e494..e915246d 100644
--- a/src/pytest_cov/plugin.py
+++ b/src/pytest_cov/plugin.py
@@ -308,7 +308,7 @@ def pytest_runtestloop(self, session):
                 warnings.warn(CovReportWarning(message))
                 self.cov_total = 0
             assert self.cov_total is not None, 'Test coverage should never be `None`'
-            if self._failed_cov_total():
+            if self._failed_cov_total() and not self.options.collectonly:
                 # make sure we get the EXIT_TESTSFAILED exit code
                 compat_session.testsfailed += 1
 
diff --git a/tests/test_pytest_cov.py b/tests/test_pytest_cov.py
index e7f90668..f93d352f 100644
--- a/tests/test_pytest_cov.py
+++ b/tests/test_pytest_cov.py
@@ -389,6 +389,19 @@ def test_cov_min_100(testdir):
     ])
 
 
+def test_cov_min_100_passes_if_collectonly(testdir):
+    script = testdir.makepyfile(SCRIPT)
+
+    result = testdir.runpytest('-v',
+                               '--cov=%s' % script.dirpath(),
+                               '--cov-report=term-missing',
+                               '--cov-fail-under=100',
+                               '--collect-only',
+                               script)
+
+    assert result.ret == 0
+
+
 def test_cov_min_50(testdir):
     script = testdir.makepyfile(SCRIPT)