Merge pull request #11244 from bluetech/rootdir-tweaks

Rootdir tweaks
This commit is contained in:
Ran Benita 2023-07-27 09:16:13 +03:00 committed by GitHub
commit 430ad145c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 15 deletions

View File

@ -46,21 +46,28 @@ Use ``""`` instead of ``''`` in expression when running this on Windows
**Run tests by node ids** **Run tests by node ids**
Each collected test is assigned a unique ``nodeid`` which consist of the module filename followed Each collected test is assigned a unique ``nodeid`` which consist of the module file path followed
by specifiers like class names, function names and parameters from parametrization, separated by ``::`` characters. by specifiers like class names and function names separated by ``::`` characters,
and parameters from parametrization in ``[...]``.
You can use the same syntax to match tests relative to the working directory.
To run a specific test within a module: To run a specific test within a module:
.. code-block:: bash .. code-block:: bash
pytest test_mod.py::test_func pytest tests/test_mod.py::test_func
To run all tests in a class:
Another example specifying a test method in the command line:
.. code-block:: bash .. code-block:: bash
pytest test_mod.py::TestClass::test_method pytest tests/test_mod.py::TestClass
Specifying a specific test method:
.. code-block:: bash
pytest tests/test_mod.py::TestClass::test_method
**Run tests by marker expressions** **Run tests by marker expressions**

View File

@ -1170,7 +1170,7 @@ class Config:
ns.inifilename, ns.inifilename,
ns.file_or_dir + unknown_args, ns.file_or_dir + unknown_args,
rootdir_cmd_arg=ns.rootdir or None, rootdir_cmd_arg=ns.rootdir or None,
config=self, invocation_dir=self.invocation_params.dir,
) )
self._rootpath = rootpath self._rootpath = rootpath
self._inipath = inipath self._inipath = inipath
@ -1243,7 +1243,7 @@ class Config:
self, self,
*, *,
args: List[str], args: List[str],
pyargs: List[str], pyargs: bool,
testpaths: List[str], testpaths: List[str],
invocation_dir: Path, invocation_dir: Path,
rootpath: Path, rootpath: Path,

View File

@ -7,7 +7,6 @@ from typing import List
from typing import Optional from typing import Optional
from typing import Sequence from typing import Sequence
from typing import Tuple from typing import Tuple
from typing import TYPE_CHECKING
from typing import Union from typing import Union
import iniconfig import iniconfig
@ -17,9 +16,6 @@ from _pytest.outcomes import fail
from _pytest.pathlib import absolutepath from _pytest.pathlib import absolutepath
from _pytest.pathlib import commonpath from _pytest.pathlib import commonpath
if TYPE_CHECKING:
from . import Config
def _parse_ini_config(path: Path) -> iniconfig.IniConfig: def _parse_ini_config(path: Path) -> iniconfig.IniConfig:
"""Parse the given generic '.ini' file using legacy IniConfig parser, returning """Parse the given generic '.ini' file using legacy IniConfig parser, returning
@ -176,8 +172,21 @@ def determine_setup(
inifile: Optional[str], inifile: Optional[str],
args: Sequence[str], args: Sequence[str],
rootdir_cmd_arg: Optional[str] = None, rootdir_cmd_arg: Optional[str] = None,
config: Optional["Config"] = None, invocation_dir: Optional[Path] = None,
) -> Tuple[Path, Optional[Path], Dict[str, Union[str, List[str]]]]: ) -> Tuple[Path, Optional[Path], Dict[str, Union[str, List[str]]]]:
"""Determine the rootdir, inifile and ini configuration values from the
command line arguments.
:param inifile:
The `--inifile` command line argument, if given.
:param args:
The free command line arguments.
:param rootdir_cmd_arg:
The `--rootdir` command line argument, if given.
:param invocation_dir:
The working directory when pytest was invoked, if known.
If not known, the current working directory is used.
"""
rootdir = None rootdir = None
dirs = get_dirs_from_args(args) dirs = get_dirs_from_args(args)
if inifile: if inifile:
@ -198,8 +207,8 @@ def determine_setup(
if dirs != [ancestor]: if dirs != [ancestor]:
rootdir, inipath, inicfg = locate_config(dirs) rootdir, inipath, inicfg = locate_config(dirs)
if rootdir is None: if rootdir is None:
if config is not None: if invocation_dir is not None:
cwd = config.invocation_params.dir cwd = invocation_dir
else: else:
cwd = Path.cwd() cwd = Path.cwd()
rootdir = get_common_ancestor([cwd, ancestor]) rootdir = get_common_ancestor([cwd, ancestor])