Skip to content

Commit

Permalink
Make zope a semi-optional test dependency
Browse files Browse the repository at this point in the history
For the admittedly small number of people who don't care about support
for `zope.interface`, it is unnecessary extra work to support even a
test-only dependency on `zope`.

The `zope`-specific tests are relatively contained, however, and with
judicious use of pytest fixtures, we can make it so that `zope` is not a
required dependency when skipping tests marked with `pytest.mark.zope`.

This allows users to test everything but the `zope`-specific tests with
no `zope` dependency with:

$ TOX_AP_TEST_EXTRAS="tests_no_zope" tox -e <env> -- -m 'not zope'
  • Loading branch information
pganssle committed Sep 2, 2020
1 parent 00c1c2c commit 4cf1da2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ multi_line_output=3
use_parentheses=true

known_first_party="attr"
known_third_party=["attr", "hypothesis", "pytest", "setuptools", "six", "zope"]
known_third_party=["attr", "hypothesis", "pytest", "setuptools", "six"]


[tool.towncrier]
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@
INSTALL_REQUIRES = []
EXTRAS_REQUIRE = {
"docs": ["sphinx", "sphinx-rtd-theme", "zope.interface"],
"tests": [
"tests_no_zope": [
# 5.0 introduced toml; parallel was broken until 5.0.2
"coverage[toml]>=5.0.2",
"hypothesis",
"pympler",
"pytest>=4.3.0", # 4.3.0 dropped last use of `convert`
"six",
"zope.interface",
],
}
EXTRAS_REQUIRE["tests"] = EXTRAS_REQUIRE["tests_no_zope"] + ["zope.interface"]
EXTRAS_REQUIRE["dev"] = (
EXTRAS_REQUIRE["tests"] + EXTRAS_REQUIRE["docs"] + ["pre-commit"]
)
Expand Down
48 changes: 30 additions & 18 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import re

import pytest
import zope.interface

import attr

Expand All @@ -29,6 +28,14 @@
from .utils import simple_attr


@pytest.fixture(scope="module")
def zope():
import zope
import zope.interface

return zope


class TestInstanceOf(object):
"""
Tests for `instance_of`.
Expand Down Expand Up @@ -217,17 +224,22 @@ class C(object):
assert C.__attrs_attrs__[0].validator == C.__attrs_attrs__[1].validator


class IFoo(zope.interface.Interface):
"""
An interface.
"""

def f():
@pytest.fixture(scope="module")
def ifoo(zope):
class IFoo(zope.interface.Interface):
"""
A function called f.
An interface.
"""

def f():
"""
A function called f.
"""

return IFoo


@pytest.mark.zope
class TestProvides(object):
"""
Tests for `provides`.
Expand All @@ -239,46 +251,46 @@ def test_in_all(self):
"""
assert provides.__name__ in validator_module.__all__

def test_success(self):
def test_success(self, zope, ifoo):
"""
Nothing happens if value provides requested interface.
"""

@zope.interface.implementer(IFoo)
@zope.interface.implementer(ifoo)
class C(object):
def f(self):
pass

v = provides(IFoo)
v = provides(ifoo)
v(None, simple_attr("x"), C())

def test_fail(self):
def test_fail(self, ifoo):
"""
Raises `TypeError` if interfaces isn't provided by value.
"""
value = object()
a = simple_attr("x")

v = provides(IFoo)
v = provides(ifoo)
with pytest.raises(TypeError) as e:
v(None, a, value)
assert (
"'x' must provide {interface!r} which {value!r} doesn't.".format(
interface=IFoo, value=value
interface=ifoo, value=value
),
a,
IFoo,
ifoo,
value,
) == e.value.args

def test_repr(self):
def test_repr(self, ifoo):
"""
Returned validator has a useful `__repr__`.
"""
v = provides(IFoo)
v = provides(ifoo)
assert (
"<provides validator for interface {interface!r}>".format(
interface=IFoo
interface=ifoo
)
) == repr(v)

Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[pytest]
addopts = -ra
testpaths = tests
markers = zope
filterwarnings =
once::Warning
ignore:::pympler[.*]
Expand Down

0 comments on commit 4cf1da2

Please sign in to comment.