Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform require_numba decorator into a global variable #245

Merged
merged 4 commits into from
Sep 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 19 additions & 27 deletions harmonica/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,29 @@
#
# This code is part of the Fatiando a Terra project (https://www.fatiando.org)
#
# pylint: disable=invalid-name
"""
Decorators and useful functions for running tests
"""
import os
import pytest

# Check if Numba is disabled
# (if NUMBA_DISABLE_JIT is not defined, we assume Numba jit is enabled)
NUMBA_IS_DISABLED = bool(os.environ.get("NUMBA_DISABLE_JIT", default="0") != "0")

def require_numba(function): # pylint: disable=unused-argument
"""
Function decorator for pytest: run if Numba jit is enabled

Functions decorator to tell pytest to run the test function only if Numba
jit is enabled. To disable Numba jit the environmental variable
```NUMBA_DISABLE_JIT``` must be set to a value different than 0.

Use this decorator on test functions that involve great computational load
and don't want to run if Numba jit is disabled. The decorated test
functions will be run and checked if pass or fail, but won't be taken into
account for meassuring coverage. If the test function will run Numba code,
but doesn't involve great computational load, we reccomend using the
``@pytest.mark.use_numba`` instead. Therefore the test function will be run
twice: one with Numba jit enabled, and another one with Numba jit disable
to check coverage.
"""
# Check if Numba is disabled
# (if NUMBA_DISABLE_JIT is not defined, we assume Numba jit is enabled)
numba_is_disabled = bool(os.environ.get("NUMBA_DISABLE_JIT", default="0") != "0")

@pytest.mark.use_numba
@pytest.mark.skipif(numba_is_disabled, reason="Numba jit is disabled")
def function_wrapper():
function()

return function_wrapper
# Decorator for pytest: run if Numba jit is enabled
#
# Tell pytest to run the test function only if Numba jit is enabled. To disable
# Numba jit the environmental variable ```NUMBA_DISABLE_JIT``` must be set to
# a value different than 0.
#
# Use this decorator on test functions that involve great computational load
# and don't want to run if Numba jit is disabled. The decorated test functions
# will be run and checked if pass or fail, but won't be taken into account for
# meassuring coverage. If the test function will run Numba code, but doesn't
# involve great computational load, we reccomend using the
# ``@pytest.mark.use_numba`` instead. Therefore the test function will be run
# twice: one with Numba jit enabled, and another one with Numba jit disable to
# check coverage.
require_numba = pytest.mark.skipif(NUMBA_IS_DISABLED, reason="Numba jit is disabled")