Test utilities and fixtures (testing)
|
A |
|
A |
|
A copy of |
|
Context which can access user's configuration, e.g. platform names. |
Shorthand for marking a parametrized test case that is expected to fail because it is not implemented. |
|
|
Mark a test as xfail or skipif if on CI infrastructure. |
Others:
Items with names that match (partially or fully) these names are omitted by |
|
|
Return or create a |
|
Export a subset of data from a scenario, for use in tests. |
|
Add two command-line options to pytest: |
- message_ix_models.testing.EXPORT_OMIT = ['aeei', 'cost_MESSAGE', 'demand_MESSAGE', 'demand', 'depr', 'esub', 'gdp_calibrate', 'grow', 'historical_gdp', 'kgdp', 'kpvs', 'lakl', 'land', 'lotol', 'mapping_macro_sector', 'MERtoPPP', 'prfconst', 'price_MESSAGE', 'ref_', 'sector'][source]
Items with names that match (partially or fully) these names are omitted by
export_test_data().
- message_ix_models.testing.KEY = {'cache-path': <_pytest.stash.StashKey object>, 'user-local-data': <_pytest.stash.StashKey object>}[source]
Keys for
pytestconfig.stash.
- message_ix_models.testing.MARK: dict[str, MarkDecorator | MarkFactory] = {'GEA_not_implemented': MarkDecorator(mark=Mark(name='xfail', args=(), kwargs={'reason': 'Not implemented with GEA input data'})), 'ISR_no_data': MarkDecorator(mark=Mark(name='xfail', args=(), kwargs={'raises': <class 'FileNotFoundError'>, 'reason': 'Missing ISR/mer-to-ppp.csv + not supported by MaybeAdaptR11Source'})), 'R12_only': MarkDecorator(mark=Mark(name='skip', args=(), kwargs={'reason': 'Currently only possible with node=R12 input data/config'})), 'R14_no_data': MarkDecorator(mark=Mark(name='xfail', args=(), kwargs={'reason': 'Missing R14 input data/config'})), 'SHAPE_not_implemented': MarkDecorator(mark=Mark(name='xfail', args=(), kwargs={'reason': 'Not implemented with SHAPE input data'})), 'ci_db_access': MarkDecorator(mark=Mark(name='xfail', args=(), kwargs={'condition': False, 'reason': 'GitHub-hosted runner has no access to IIASA-internal databases'})), 'ci_linux_only': MarkDecorator(mark=Mark(name='skipif', args=(), kwargs={'condition': False, 'reason': 'Skip on non-Linux GitHub Actions runners, for performance'})), 'ci_not_macos_intel': MarkDecorator(mark=Mark(name='skipif', args=(), kwargs={'condition': False, 'reason': 'R not installed on macos-15-intel GitHub Actions runners'})), 'ci_timeout': MarkDecorator(mark=Mark(name='skipif', args=(False,), kwargs={'reason': 'Stalls or times out on GitHub Actions'})), 'gh_375': MarkDecorator(mark=Mark(name='flaky', args=(), kwargs={'reruns': 3, 'rerun_delay': 2, 'condition': False, 'reason': 'https://github.com/iiasa/message-ix-models/issues/375'})), 'gh_471': MarkDecorator(mark=Mark(name='xfail', args=(), kwargs={'condition': False, 'reason': 'Temporary, for https://github.com/iiasa/message-ix-models/pull/471'})), 'ixmp_595': MarkDecorator(mark=Mark(name='xfail', args=(), kwargs={'condition': True, 'reason': 'https://github.com/iiasa/ixmp#595'})), 'ixmp_600': MarkDecorator(mark=Mark(name='xfail', args=(), kwargs={'condition': False, 'reason': 'https://github.com/iiasa/ixmp/issues/600'})), 'message_ix_cap_comm': MarkDecorator(mark=Mark(name='xfail', args=(True,), kwargs={'reason': 'Requires MESSAGE cap-comm parameters, only available in message-ix >=3.12'})), 'needs_message_data': MarkDecorator(mark=Mark(name='xfail', args=(), kwargs={'condition': True, 'reason': 'Not yet migrated from message_data'})), 'no_data': <message_ix_models.util.pytest.MarkFactory object>, 'non_public_data': <message_ix_models.util.pytest.MarkFactory object>, 'sdmx_230': MarkDecorator(mark=Mark(name='xfail', args=(), kwargs={'condition': False, 'reason': 'https://github.com/khaeru/sdmx/issues/230', 'raises': <class 'sdmx.exceptions.XMLParseError'>})), 'transport_build_data': MarkDecorator(mark=Mark(name='usefixtures', args=('advance_test_data', 'iea_eweb_test_data', 'ssp_user_data'), kwargs={})), 'zenodo_timeout': MarkDecorator(mark=Mark(name='xfail', args=(), kwargs={'condition': False, 'reason': 'Connection to Zenodo times out', 'raises': <class 'requests.exceptions.ReadTimeout'>}))}[source]
Common marks for tests. Use short, informative keys that are valid identifiers; these can then be applied using, for instance,
@pytest.mark.ci_db_access.
- message_ix_models.testing.NIE = MarkDecorator(mark=Mark(name='xfail', args=(), kwargs={'raises': <class 'NotImplementedError'>}))[source]
Shorthand for marking a parametrized test case that is expected to fail because it is not implemented.
- message_ix_models.testing.advance_test_data(monkeypatch) None[source]
Temporarily allow
path_fallback()to find test data.
- message_ix_models.testing.bare_res(request: FixtureRequest, context: Context, solved: bool = False) Scenario[source]
Return or create a
Scenariocontaining the bare RES for use in testing.The Scenario has a model name like “MESSAGEix-GLOBIOM [regions] Y[years]”, for instance “MESSAGEix-GLOBIOM R14 YB” (see
bare.name()) and a scenario name either fromrequest.node.nameor “baseline” plus a random string of 5 characters.This function should:
only be called from within test code, i.e. in
message_data.tests.be called once for each test function, so that each test receives a fresh copy of the RES scenario.
- Parameters:
request (
FixtureRequestorNone) – The pytestrequestfixture. If provided the pytest test node name is used for the scenario name of the returned Scenario.context (
Context) – Passed totesting.bare_res().solved (
bool, optional) – Return a solved Scenario.
- Returns:
The scenario is a fresh clone, so can be modified freely without disturbing other tests.
- Return type:
- message_ix_models.testing.export_test_data(context: Context)[source]
Export a subset of data from a scenario, for use in tests.
The context settings
export_nodes(default: “R11_AFR” and “R11_CPA”) andexport_techs(default: “coal_ppl”) are used to filter the data exported. In addition, any item (set, parameter, variable, or equation) with a name matchingEXPORT_OMITor the context settingexport_excludeis discarded.The output is stored at
data/tests/model name_scenario name_techs.xlsxinmessage_data.See also
- message_ix_models.testing.iea_eei_user_data(pytestconfig, monkeypatch) None[source]
Temporarily allow
IEA_EEIto find user data.
- message_ix_models.testing.iea_eweb_test_data(monkeypatch, session_context) None[source]
Temporarily allow
path_fallback()to find test data.session_context is required in order to set the cache path in the test environment.
- message_ix_models.testing.iea_eweb_user_data(pytestconfig, monkeypatch) None[source]
Temporarily allow
IEA_EWEBto find user data.
- message_ix_models.testing.mix_models_cli(session_context, tmp_env)[source]
A
CliRunnerobject that invokes the mix-models CLI.NB this requires:
The
ixmptmp_env()fixture. This setsIXMP_DATAto a temporary directory managed bypytest.The
session_context()fixture. This (a) setsConfig.local_datato a temporary directory withinIXMP_DATAand (b) ensures changes toContextmade by invoked commands do not reach other tests.
- message_ix_models.testing.not_ci(reason=None, action='skip')[source]
Mark a test as xfail or skipif if on CI infrastructure.
Checks the
GITHUB_ACTIONSenvironment variable; returns a pytest mark.
- message_ix_models.testing.pytest_addoption(parser)[source]
Add two command-line options to pytest:
--local-cacheUse existing, local cache files in tests. This option can speed up tests that use the results of slow data loading/parsing. However, if cached values are not up to date with the current code, unexpected failure may occur.
--jvmargsAdditional arguments to give for the Java Virtual Machine used by
ixmp’sJDBCBackend. Used bysession_context().
- message_ix_models.testing.pytest_collection_modifyitems(session: Session, config: Config, items: list[Item]) None[source]
Substitute or add marks from
MARK.
- message_ix_models.testing.pytest_configure(config: Config) None[source]
Register markers for keys in
MARK.
- message_ix_models.testing.pytest_report_header(config, start_path) str[source]
Add the ixmp configuration to the pytest report header.
- message_ix_models.testing.pytest_sessionstart(session: Session) None[source]
Identify the path to be used for cached local data.
If pytest --local-cache was given, pick up the existing setting from the user’s environment. Otherwise, use a pytest-managed cache directory that persists across test sessions.
- message_ix_models.testing.session_context(pytestconfig, tmp_env)[source]
A
Contextconnected to a temporary, in-memory database.This Context is suitable for modifying and running test code that does not affect the user/developer’s filesystem and configured
ixmpdatabases.Uses the
tmp_env()fixture from ixmp. This fixture also sets:Config.cache_path, depending on whether the --local-cache CLI option was given:If not given: pytest’s standard cache directory.
If given: the
/cache/directory under the user’s “message local data” directory.
the “message local data” config key to a temporary directory
/data/under the pytest tmp_path directory.
- message_ix_models.testing.ssp_test_data(monkeypatch) None[source]
Temporarily allow
path_fallback()to find test data.
- message_ix_models.testing.ssp_user_data(pytestconfig, monkeypatch) None[source]
Temporarily allow
SSPOriginal/SSPUpdateto find user data.
- message_ix_models.testing.test_context(request, session_context)[source]
A copy of
session_context()scoped to one test function.
- message_ix_models.testing.test_data_path() Traversable[source]
Path to the test data directory,
message_ix_models/data/tests/.
- message_ix_models.testing.unpack_snapshot_data(context: Context, snapshot_id: int)[source]
Already-unpacked data for a snapshot.
This copies the .csv.gz files from message_ix_models/data/test/… to the directory where they would be unpacked by .model.snapshot._unpack. This causes the code to skip unpacking them, which can be very slow.
- message_ix_models.testing.user_context(request)[source]
Context which can access user’s configuration, e.g. platform names.
Checks
In-line checks for genno graphs.
- class message_ix_models.testing.check.CheckResult[source]
Accumulator for the results of multiple checks.
This class’
__call__()method can be used as theresult_cbargument toapply_checks(). After doing so,bool(check_result)will give the overall passage or failure of the check suite.- assert_all_passed() None[source]
Raise
AssertionErrorif not all checks passed.
- class message_ix_models.testing.check.ContainsDataForParameters(parameter_names: set[str])[source]
- class message_ix_models.testing.check.Dump(base_path: Path)[source]
Dump to a temporary path for inspection.
This always returns
True.
- class message_ix_models.testing.check.HasCoords(coords: dict[str, Collection[Hashable]], inverse: bool = False)[source]
Object has/omits certain coordinates.
- class message_ix_models.testing.check.HasUnits(units: str | dict | pint.registry.Quantity | pint.registry.Unit | None)[source]
Quantity has the expected units.
- class message_ix_models.testing.check.InRange(min: float = -inf, max: float = inf, atol: float = 1e-08)[source]
Values are in expected range.
- class message_ix_models.testing.check.Log(rows: int | None = 7)[source]
Log contents.
This always returns
True.
- class message_ix_models.testing.check.NoDuplicates(setting: None = None, rows: int = 7)[source]
No duplicate indices in parameter data.
- class message_ix_models.testing.check.NonNegative[source]
No negative values.
Todo
Add args so the check can be above or below any threshold value.
- class message_ix_models.testing.check.Size(setting: dict[str, int])[source]
Quantity has expected size on certain dimensions.
- message_ix_models.testing.check.apply_checks(value: T, checks: Collection[Check], *, key: KeyLike, result_cb: Callable[[bool, str], None]) T[source]
Apply some checks to value.
- message_ix_models.testing.check.insert_checks(computer: genno.Computer, target: KeyLike, check_map: Mapping[KeyLike, Collection[Check]], check_common: Collection[Check]) CheckResult[source]
Insert some
Checksinto computer.- Parameters:
computer
target – A new key added to trigger all the tasks and checks.
check_map – A mapping from existing keys (that must appear in computer) to collections of
Checkinstances to be applied to those keys.check_common – A collection of common
Checkinstances, to be applied to every key in check_map.
- Returns:
after the checks are triggered (for instance, with
computer.get(target)), this object will contain the overall check pass/fail result.- Return type:
- message_ix_models.testing.check.verbose_check(verbosity: int, tmp_path: Path | None = None) list[Check][source]
Return 0 or more checks that display the data to which they are applied.
These may be appended to collections passed as inputs to
insert_checks().- Parameters:
verbosity (
int) –Don’t log anything
Log
Log.rowsvalues at the start/end of each quantity.Log all data. This can produce large logs, e.g. more than 1 GiB of text for
tests.model.transport.test_build.test_debug().Dump all data to files in tmp_path.