Skip to content

Commit

Permalink
Migrate to pytest and support PY3 syntax on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drslump committed Sep 16, 2016
1 parent 34a7975 commit 46055e1
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 12 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ python:
- "2.7"
- "3.3"
- "3.4"
- "3.5.1"
- "pypy"

# command to install dependencies
install:
- "pip install ."
- "pip install nose"
- "pip install pyshould"

# command to run tests
script: nosetests
script: python setup.py test

3 changes: 3 additions & 0 deletions di/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ def __init__(self, value, *values):
else:
self.value = value

def __hash__(self):
return hash(self.value)

def __eq__(self, other):
if isinstance(other, Key):
return self.value == other.value
Expand Down
7 changes: 7 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@
license = GNU AFFERO GENERAL version 3
description-file = README.md
license-file = LICENSE

[aliases]
test=pytest

[tool:pytest]
# Make sure we only include those files suffixed with -tests
python_files = tests/*_tests.py
7 changes: 4 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
from setuptools import setup, find_packages

try:
Expand All @@ -17,8 +18,8 @@
name='di-py',
packages=find_packages(exclude=['test*']),
url='https://www.github.com/telefonicaid/di-py',
tests_require=['nose', 'pyshould'],
test_suite='nose.collector',
version='1.1.0',
setup_requires=['pytest-runner'] if 'test' in sys.argv else [],
tests_require=['pytest', 'pyshould'],
version='1.1.1',
zip_safe=False,
)
16 changes: 10 additions & 6 deletions tests/di_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@
:license: see LICENSE for more details.
"""

import sys
import warnings

import unittest
import pytest
from pyshould import should

from di import injector, Key, DependencyMap, ContextualDependencyMap, PatchedDependencyMap, MetaInject

PY3 = sys.version_info[0] >= 3

# Import tests using Python3 syntax
if PY3:
from .py3 import *


class Ham(object):
pass
Expand Down Expand Up @@ -107,6 +115,7 @@ def test_no_injectable_params(self):
foo = self.inject(lambda x: x, warn=False)
foo(10) | should.be(10)

@pytest.mark.skipif(PY3, reason='Python 3 deprecates getargspec generating an extra warning')
def test_warns_when_unneeded(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
Expand All @@ -124,15 +133,10 @@ def test_descriptor_no_type_found(self):
foo.bar()


@pytest.mark.skipif(PY3, reason='requires python 2.x (meta-class syntax changes)')
class InjectorMetaclassTests(unittest.TestCase):

def setUp(self):
# Python 3 metaclass syntax is not compatible with Python 2.x
import sys
if sys.version_info[0] >= 3:
from nose.plugins.skip import SkipTest
raise SkipTest

self.ham = Ham()
self.map = {
Ham: self.ham
Expand Down
32 changes: 32 additions & 0 deletions tests/py3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
This file contains tests that use PY3 syntax and would break the parser
if loaded with PY2.
Make sure the test runner doesn't collect this file automatically and that
the symbols in this module are not shadowed by the ones in the main test file.
:copyright: (c) 2013 by Telefonica I+D.
:license: see LICENSE for more details.
"""

import unittest
from pyshould import should

from di import injector, Key

KeyA = Key('A')
KeyB = Key('B')
KeyC = Key('C')


def test_py3_kwonly():

inject = injector({KeyA: 'A', KeyB: 'B', KeyC: 'C'})

# While we don't fix the inject decorator we'll receive an error
with should.throw(ValueError):
@inject
def foo(a, b=KeyB, *args, c=KeyC):
pass


0 comments on commit 46055e1

Please sign in to comment.