Skip to content

Commit

Permalink
add new test target to skip stripe-mock dependent tests (#672)
Browse files Browse the repository at this point in the history
* add new test target to skip stripe-mock dependent tests

* resolve pytest version conflict for python >= 3.5.*

* use pytest's fixturesnames instead of funcargnames

* reformat conftest.py
  • Loading branch information
suz-stripe authored Aug 25, 2020
1 parent 54bada3 commit b05a7fc
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ $(VENV_NAME)/bin/activate: setup.py
test: venv
@${VENV_NAME}/bin/tox -p auto $(TOX_ARGS)

test-nomock: venv
@${VENV_NAME}/bin/tox -p auto -- --nomock $(TOX_ARGS)

ci: venv
@${VENV_NAME}/bin/python setup.py test -a "-n auto --cov=stripe"

Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def run_tests(self):
],
python_requires=">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*",
tests_require=[
"pytest >= 4.6.2, < 4.7",
'pytest >= 4.6.2, < 4.7; python_version < "3.5"',
'pytest >= 6.0.0; python_version >= "3.5"',
"pytest-mock >= 2.0.0",
"pytest-xdist >= 1.31.0",
"pytest-cov >= 2.8.1",
Expand Down
2 changes: 1 addition & 1 deletion tests/api_resources/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_is_detachable_when_attached(self, request_mock):
"delete", "/v1/customers/cus_123/sources/%s" % TEST_RESOURCE_ID
)

def test_is_not_detachable_when_unattached(self):
def test_is_not_detachable_when_unattached(self, request_mock):
resource = stripe.Source.retrieve(TEST_RESOURCE_ID)
with pytest.raises(stripe.error.InvalidRequestError):
resource.detach()
Expand Down
58 changes: 39 additions & 19 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,47 @@ def stop_stripe_mock():
StripeMock.stop()


try:
resp = urlopen("http://localhost:%s/" % MOCK_PORT)
info = resp.info()
except HTTPError as e:
info = e.info()
except Exception:
sys.exit(
"Couldn't reach stripe-mock at `localhost:%s`. Is "
"it running? Please see README for setup instructions." % MOCK_PORT
def pytest_configure(config):
if not config.getoption("--nomock"):
try:
resp = urlopen("http://localhost:%s/" % MOCK_PORT)
info = resp.info()
version = info.get("Stripe-Mock-Version")
if version != "master" and StrictVersion(version) < StrictVersion(
MOCK_MINIMUM_VERSION
):
sys.exit(
"Your version of stripe-mock (%s) is too old. The minimum "
"version to run this test suite is %s. Please "
"see its repository for upgrade instructions."
% (version, MOCK_MINIMUM_VERSION)
)

except HTTPError as e:
info = e.info()
except Exception:
sys.exit(
"Couldn't reach stripe-mock at `localhost:%s`. Is "
"it running? Please see README for setup instructions."
% MOCK_PORT
)


def pytest_addoption(parser):
parser.addoption(
"--nomock",
action="store_true",
help="only run tests that don't need stripe-mock",
)

version = info.get("Stripe-Mock-Version")
if version != "master" and StrictVersion(version) < StrictVersion(
MOCK_MINIMUM_VERSION
):
sys.exit(
"Your version of stripe-mock (%s) is too old. The minimum "
"version to run this test suite is %s. Please "
"see its repository for upgrade instructions."
% (version, MOCK_MINIMUM_VERSION)
)

def pytest_runtest_setup(item):
if "request_mock" in item.fixturenames and item.config.getoption(
"--nomock"
):
pytest.skip(
"run stripe-mock locally and remove --nomock flag to run skipped tests"
)


@pytest.fixture(autouse=True)
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ skip_install = true
description = run static analysis and style check using flake8
basepython = python2.7
deps = flake8
commands = python -m flake8 --show-source stripe tests setup.py {posargs}
commands = python -m flake8 --show-source stripe tests setup.py
skip_install = true

0 comments on commit b05a7fc

Please sign in to comment.