@@ -428,8 +428,35 @@ def test(
428428 """ # noqa: E501
429429 cfg = get_config ()
430430 distname = cfg .get ("project.name" , None )
431+ pytest_args = pytest_args or ()
431432
432- if gcov and distname and _is_editable_install_of_same_source (distname ):
433+ # User specified tests without -t flag
434+ # Rewrite arguments as though they specified using -t and proceed
435+ if (len (pytest_args ) == 1 ) and (not tests ):
436+ tests = pytest_args [0 ]
437+ pytest_args = ()
438+
439+ package = cfg .get ("tool.spin.package" , None )
440+ if package is None :
441+ print (
442+ "Please specify `package = packagename` under `tool.spin` section of `pyproject.toml`"
443+ )
444+ raise SystemExit (1 )
445+
446+ # User did not specify what to test, so we test
447+ # the full package
448+ if not (pytest_args or tests ):
449+ pytest_args = ("--pyargs" , package )
450+ elif tests :
451+ if (os .path .sep in tests ) or ("/" in tests ):
452+ # Tests specified as file path
453+ pytest_args = pytest_args + (tests ,)
454+ else :
455+ # Otherwise tests given as modules
456+ pytest_args = pytest_args + ("--pyargs" , tests )
457+
458+ is_editable_install = distname and _is_editable_install_of_same_source (distname )
459+ if gcov and is_editable_install :
433460 click .secho (
434461 "Error: cannot generate coverage report for editable installs" ,
435462 fg = "bright_red" ,
@@ -446,16 +473,6 @@ def test(
446473 else :
447474 ctx .invoke (build_cmd )
448475
449- package = cfg .get ("tool.spin.package" , None )
450- if package is None :
451- print (
452- "Please specify `package = packagename` under `tool.spin` section of `pyproject.toml`"
453- )
454- raise SystemExit (1 )
455-
456- if (not pytest_args ) and (not tests ):
457- tests = package
458-
459476 site_path = _set_pythonpath ()
460477 if site_path :
461478 print (f'$ export PYTHONPATH="{ site_path } "' )
@@ -479,8 +496,8 @@ def test(
479496 if (n_jobs != "1" ) and ("-n" not in pytest_args ):
480497 pytest_args = ("-n" , str (n_jobs )) + pytest_args
481498
482- if tests and "--pyargs" not in pytest_args :
483- pytest_args = ("--pyargs" , tests ) + pytest_args
499+ if not any ( "--import-mode" in arg for arg in pytest_args ) :
500+ pytest_args = ("--import-mode=importlib" , ) + pytest_args
484501
485502 if verbose :
486503 pytest_args = ("-v" ,) + pytest_args
@@ -503,9 +520,12 @@ def test(
503520 else :
504521 cmd = ["pytest" ]
505522
506- pytest_p = _run (
507- cmd + list (pytest_args ),
508- )
523+ if not os .path .exists (install_dir ):
524+ os .mkdir (install_dir )
525+
526+ cwd = os .getcwd ()
527+ pytest_p = _run (cmd + list (pytest_args ), cwd = site_path )
528+ os .chdir (cwd )
509529
510530 if gcov :
511531 # Verify the tools are present
0 commit comments