11"""Configuration for pytest."""
22
3+ import os
4+
35import pytest
46
57
8+ def _use_dask_array (config : pytest .Config ) -> bool :
9+ env_value = os .environ .get ("XR_USE_DASK_ARRAY_WITH_EXPR" , "" )
10+ return config .getoption ("--use-dask-array-with-expr" ) or env_value .lower () in {
11+ "1" ,
12+ "true" ,
13+ "yes" ,
14+ "on" ,
15+ }
16+
17+
18+ def _register_dask_array () -> None :
19+ try :
20+ import dask_array .xarray
21+ except ImportError as err :
22+ raise pytest .UsageError (
23+ "--use-dask-array-with-expr requires dask-array to be importable"
24+ ) from err
25+
26+ dask_array .xarray .register ()
27+ if not dask_array .xarray .isactive ():
28+ raise pytest .UsageError (
29+ "--use-dask-array-with-expr registered dask-array, but it is not the active dask chunk manager"
30+ )
31+
32+
633def pytest_addoption (parser : pytest .Parser ):
734 """Add command-line flags for pytest."""
835 parser .addoption ("--run-flaky" , action = "store_true" , help = "runs flaky tests" )
@@ -12,9 +39,31 @@ def pytest_addoption(parser: pytest.Parser):
1239 help = "runs tests requiring a network connection" ,
1340 )
1441 parser .addoption ("--run-mypy" , action = "store_true" , help = "runs mypy tests" )
42+ parser .addoption (
43+ "--use-dask-array-with-expr" ,
44+ action = "store_true" ,
45+ help = "register dask-array as xarray's dask chunk manager" ,
46+ )
47+
48+
49+ def pytest_configure (config : pytest .Config ):
50+ config .addinivalue_line (
51+ "markers" ,
52+ "skip_with_dask_array: skip when dask-array is registered as xarray's dask chunk manager" ,
53+ )
54+ config .addinivalue_line (
55+ "markers" ,
56+ "xfail_with_dask_array: xfail when dask-array is registered as xarray's dask chunk manager" ,
57+ )
58+ if not _use_dask_array (config ):
59+ return
60+
61+ _register_dask_array ()
1562
1663
1764def pytest_runtest_setup (item ):
65+ if _use_dask_array (item .config ):
66+ _register_dask_array ()
1867 # based on https://stackoverflow.com/questions/47559524
1968 if "flaky" in item .keywords and not item .config .getoption ("--run-flaky" ):
2069 pytest .skip ("set --run-flaky option to run flaky tests" )
@@ -39,6 +88,18 @@ def pytest_collection_modifyitems(items):
3988 # marking approach, meaning that each test case must contain "mypy" in the
4089 # name.
4190 item .add_marker (pytest .mark .mypy )
91+ if _use_dask_array (item .config ) and "skip_with_dask_array" in item .keywords :
92+ item .add_marker (
93+ pytest .mark .skip (reason = "skipped with dask-array chunk manager" )
94+ )
95+ if _use_dask_array (item .config ) and "xfail_with_dask_array" in item .keywords :
96+ mark = item .get_closest_marker ("xfail_with_dask_array" )
97+ kwargs = dict (mark .kwargs ) if mark is not None else {}
98+ kwargs .setdefault (
99+ "reason" , "expected failure with dask-array chunk manager"
100+ )
101+ kwargs .setdefault ("strict" , True )
102+ item .add_marker (pytest .mark .xfail (** kwargs ))
42103
43104
44105@pytest .fixture (autouse = True )
0 commit comments