1+ from os import environ
12from pathlib import Path
23
34import nox
45
56
67package_path = Path (__file__ ).parent
8+ is_circleci = 'CIRCLECI' in environ
9+
710nox .options .default_venv_backend = 'uv'
811
912
1013@nox .session
1114def pytest (session : nox .Session ):
12- uv_sync (session , 'pytest' )
15+ uv_sync (session )
1316 pytest_run (session )
1417
1518
@@ -26,45 +29,78 @@ def precommit(session: nox.Session):
2629@nox .session
2730def audit (session : nox .Session ):
2831 # Much faster to install the deps first and have pip-audit run against the venv
29- uv_sync (session , 'audit' )
32+ uv_sync (session )
3033 session .run (
3134 'pip-audit' ,
3235 '--desc' ,
3336 '--skip-editable' ,
3437 )
3538
3639
40+ def pytest_run (session : nox .Session , * args , ** env ):
41+ """
42+ Using functions for pytest and uv enables more advanced uses cases. Examples:
43+
44+ py_all = ['3.10', '3.11', '3.12', '3.13']
45+
46+ @session(py=py_all)
47+ @parametrize('db', ['pg', 'sqlite'])
48+ def pytest(session: Session, db: str):
49+ uv_sync(session)
50+ pytest_run(session, WEBTEST_DB=db)
51+
52+
53+ @session(py=py_single)
54+ def pytest_mssql(session: Session):
55+ uv_sync(session, 'pytest', 'mssql')
56+ pytest_run(session, WEBTEST_DB='mssql')
57+
58+
59+ @session(py=py_single)
60+ def pytest_i18n(session: Session):
61+ uv_sync(session, extra='i18n')
62+ pytest_run(session, WEBTEST_DB='sqlite')
63+
64+ """
65+ junit_opt = f'--junit-xml={ package_path } /ci/test-reports/{ session .name } .pytests.xml'
66+ junit_args = (junit_opt ,) if is_circleci else ()
67+
68+ session .run (
69+ 'pytest' ,
70+ '-ra' ,
71+ '--tb=native' ,
72+ '--strict-markers' ,
73+ '--cov' ,
74+ '--cov-report=xml' ,
75+ '--no-cov-on-fail' ,
76+ * junit_args ,
77+ package_path / 'tests' ,
78+ * args ,
79+ * session .posargs ,
80+ env = env ,
81+ )
82+
83+
3784def uv_sync (session : nox .Session , * groups , project = False , extra = None ):
3885 if not groups :
3986 groups = (session .name ,)
87+
88+ # At least pytest needs the project installed.
4089 project_args = () if project or session .name .startswith ('pytest' ) else ('--no-install-project' ,)
90+
4191 group_args = [arg for group in groups for arg in ('--group' , group )]
4292 extra_args = ('--extra' , extra ) if extra else ()
93+
4394 run_args = (
4495 'uv' ,
4596 'sync' ,
4697 '--active' ,
98+ '--exact' ,
99+ # Use --no-default-groups instead of --only-group as the latter implies
100+ # --no-install-project.
47101 '--no-default-groups' ,
48102 * project_args ,
49103 * group_args ,
50104 * extra_args ,
51105 )
52106 session .run (* run_args )
53-
54-
55- def pytest_run (session : nox .Session , * args , ** env ):
56- session .run (
57- 'pytest' ,
58- '-ra' ,
59- '--tb=native' ,
60- '--strict-markers' ,
61- '--cov' ,
62- '--cov-config=.coveragerc' ,
63- f'--junit-xml={ package_path } /ci/test-reports/{ session .name } .pytests.xml' ,
64- f'--cov-report=xml:{ package_path } /ci/coverage/{ session .name } .xml' ,
65- '--no-cov-on-fail' ,
66- package_path / 'tests' ,
67- * args ,
68- * session .posargs ,
69- env = env ,
70- )
0 commit comments