2022-10-14 02:56:17 +00:00
|
|
|
"""
|
|
|
|
This file will not be automatically imported by `colossalai.testing`
|
2023-06-27 03:02:25 +00:00
|
|
|
as this file has a dependency on `pytest`. Therefore, you need to
|
2022-10-14 02:56:17 +00:00
|
|
|
explicitly import this file `from colossalai.testing.pytest_wrapper import <func>`.from
|
|
|
|
"""
|
|
|
|
|
2022-10-13 11:38:45 +00:00
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
|
|
def run_on_environment_flag(name: str):
|
|
|
|
"""
|
|
|
|
Conditionally run a test based on the environment variable. If this environment variable is set
|
|
|
|
to 1, this test will be executed. Otherwise, this test is skipped. The environment variable is default to 0.
|
2022-10-14 02:56:17 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
name (str): the name of the environment variable flag.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
# in your pytest file
|
|
|
|
@run_on_environment_flag(name='SOME_FLAG')
|
|
|
|
def test_for_something():
|
|
|
|
do_something()
|
|
|
|
|
|
|
|
# in your terminal
|
|
|
|
# this will execute your test
|
|
|
|
SOME_FLAG=1 pytest test_for_something.py
|
|
|
|
|
|
|
|
# this will skip your test
|
|
|
|
pytest test_for_something.py
|
|
|
|
|
2022-10-13 11:38:45 +00:00
|
|
|
"""
|
2023-06-27 03:02:25 +00:00
|
|
|
try:
|
|
|
|
import pytest
|
|
|
|
except ImportError:
|
|
|
|
raise ImportError(
|
2023-09-19 06:20:26 +00:00
|
|
|
"This function requires `pytest` to be installed, please do `pip install pytest` and try again."
|
|
|
|
)
|
2023-06-27 03:02:25 +00:00
|
|
|
|
2022-10-13 11:38:45 +00:00
|
|
|
assert isinstance(name, str)
|
2023-09-19 06:20:26 +00:00
|
|
|
flag = os.environ.get(name.upper(), "0")
|
2022-10-13 11:38:45 +00:00
|
|
|
|
2023-09-19 06:20:26 +00:00
|
|
|
reason = f"Environment variable {name} is {flag}"
|
|
|
|
if flag == "1":
|
2022-10-13 11:38:45 +00:00
|
|
|
return pytest.mark.skipif(False, reason=reason)
|
|
|
|
else:
|
|
|
|
return pytest.mark.skipif(True, reason=reason)
|