Skip to content

Commit

Permalink
Transform require_numba decorator into a global variable (#245)
Browse files Browse the repository at this point in the history
The require_numba as a function wrapper makes it hard to use
pytest.mark.parametrize on test functions. Following pytest
documentation, it's better to define custom decorators as global
variables.
  • Loading branch information
santisoler authored Sep 13, 2021
1 parent aed37e3 commit d4222ef
Showing 1 changed file with 19 additions and 27 deletions.
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")

0 comments on commit d4222ef

Please sign in to comment.