Skip to content

Commit

Permalink
TEST: warn user if gurobipy not installed
Browse files Browse the repository at this point in the history
skipif() has the advantage of explicitly warning the user if tests
are not included due to gurobipy not being installed. This is entirely
similar to behavior in plot_test.py with matplotlib.

This changeset replaces the `nonfree` marker, which was not friendly
to newcomers: they would need to know to `pytest -m nonfree` if they
wanted to test with Gurobi. Otherwise, `pytest` (without the marker)
would silently not include those tests.

The advantages of using the `nonfree` marker are:
1. emphasizing to the user that tests involve behavior by code without
   open source,
2. failing if the corresponding packages (gurobipy, possibly others in
   the future) are not installed.
However, users who know enough to use `pytest -m nonfree` likely are
aware of Gurobi's license and likely would notice a warning when tests
are running, so indeed, skipif() is the best choice.
  • Loading branch information
slivingston committed Oct 2, 2024
1 parent 3cc3a74 commit 2af6893
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
15 changes: 2 additions & 13 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,10 @@ jobs:
export CVXOPT_BUILD_GLPK=1
pip install cvxopt
python -c 'import cvxopt.glpk'
- name: Run all tests, using `cvxopt.glpk`
run: |
set -o posix
echo 'Exported environment variables:'
export -p
cd tests/
pytest \
-v \
--continue-on-collection-errors \
.
- name: Install `gurobipy` (nonfree)
- name: Install optional solvers with restrictive licenses
run: |
pip install gurobipy
- name: Run tests involving nonfree solvers
- name: Run all tests, using `cvxopt.glpk`
run: |
set -o posix
echo 'Exported environment variables:'
Expand All @@ -113,5 +103,4 @@ jobs:
pytest \
-v \
--continue-on-collection-errors \
-m nonfree \
.
9 changes: 8 additions & 1 deletion tests/polytope_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
import pytest
import scipy.optimize

try:
import gurobipy
except ImportError:
gurobipy = None

import polytope as pc
import polytope.polytope as alg
from polytope import solvers
Expand Down Expand Up @@ -616,7 +621,9 @@ def test_reduce():
assert_allclose(u, np.array([[50.], [1.]]), rtol=1e-07, atol=1e-07)


@pytest.mark.nonfree
@pytest.mark.skipif(
gurobipy is None,
reason='`gurobipy` is not installed')
def test_gurobipy_return_same_result_as_scipy():
c, A, b = example_1d()
result_gurobi = solvers.lpsolve(c, A, b, solver='gurobi')
Expand Down
4 changes: 1 addition & 3 deletions tests/pytest.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
[pytest]
addopts = --strict-markers -m 'not nonfree'
markers =
nonfree: marks tests as requiring nonfree (i.e., having restrictive licenses) dependencies
addopts = --strict-markers

0 comments on commit 2af6893

Please sign in to comment.