@@ -425,8 +425,35 @@ def test(
425425 """ # noqa: E501
426426 cfg = get_config ()
427427 distname = cfg .get ("project.name" , None )
428+ pytest_args = pytest_args or ()
428429
429- if gcov and distname and _is_editable_install_of_same_source (distname ):
430+ # User specified tests without -t flag
431+ # Rewrite arguments as though they specified using -t and proceed
432+ if (len (pytest_args ) == 1 ) and (not tests ):
433+ tests = pytest_args [0 ]
434+ pytest_args = ()
435+
436+ package = cfg .get ("tool.spin.package" , None )
437+ if package is None :
438+ print (
439+ "Please specify `package = packagename` under `tool.spin` section of `pyproject.toml`"
440+ )
441+ raise SystemExit (1 )
442+
443+ # User did not specify what to test, so we test
444+ # the full package
445+ if not (pytest_args or tests ):
446+ pytest_args = ("--pyargs" , package )
447+ elif tests :
448+ if (os .path .sep in tests ) or ("/" in tests ):
449+ # Tests specified as file path
450+ pytest_args = pytest_args + (tests ,)
451+ else :
452+ # Otherwise tests given as modules
453+ pytest_args = pytest_args + ("--pyargs" , tests )
454+
455+ is_editable_install = distname and _is_editable_install_of_same_source (distname )
456+ if gcov and is_editable_install :
430457 click .secho (
431458 "Error: cannot generate coverage report for editable installs" ,
432459 fg = "bright_red" ,
@@ -443,16 +470,6 @@ def test(
443470 else :
444471 ctx .invoke (build_cmd )
445472
446- package = cfg .get ("tool.spin.package" , None )
447- if package is None :
448- print (
449- "Please specify `package = packagename` under `tool.spin` section of `pyproject.toml`"
450- )
451- raise SystemExit (1 )
452-
453- if (not pytest_args ) and (not tests ):
454- tests = package
455-
456473 site_path = _set_pythonpath ()
457474 if site_path :
458475 print (f'$ export PYTHONPATH="{ site_path } "' )
@@ -476,8 +493,8 @@ def test(
476493 if (n_jobs != "1" ) and ("-n" not in pytest_args ):
477494 pytest_args = ("-n" , str (n_jobs )) + pytest_args
478495
479- if tests and "--pyargs" not in pytest_args :
480- pytest_args = ("--pyargs" , tests ) + pytest_args
496+ if not any ( "--import-mode" in arg for arg in pytest_args ) :
497+ pytest_args = ("--import-mode=importlib" , ) + pytest_args
481498
482499 if verbose :
483500 pytest_args = ("-v" ,) + pytest_args
@@ -500,9 +517,12 @@ def test(
500517 else :
501518 cmd = ["pytest" ]
502519
503- pytest_p = _run (
504- cmd + list (pytest_args ),
505- )
520+ if not os .path .exists (install_dir ):
521+ os .mkdir (install_dir )
522+
523+ cwd = os .getcwd ()
524+ pytest_p = _run (cmd + list (pytest_args ), cwd = site_path )
525+ os .chdir (cwd )
506526
507527 if gcov :
508528 # Verify the tools are present
0 commit comments