diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000000000000000000000000000000000000..3ec7043da3b152146e8ec712665323ccef1692d4 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,32 @@ +name: CI + +on: + push: + pull_request: + workflow_dispatch: # For manual triggering + schedule: # Runs on default branch only + - cron: '12 12 * * 0' + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + matrix: + python-version: ["3.7", "3.8", "3.9", "3.10"] + + steps: + - uses: actions/checkout@v3 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: ${{ matrix.python-version }} + - name: Install dependencies + run: make env + - name: Test with pytest + run: make test + - name: Publish package + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags') + uses: pypa/gh-action-pypi-publish@release/v1 + with: + password: ${{ secrets.PYPI_API_TOKEN }} diff --git a/.gitignore b/.gitignore index f3298a08e84e0295056b5db172c26d02c6010f55..276825e5b52c46ac8d0d96708cc9fb7542ae9484 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,8 @@ pip-log.txt .#* MANIFEST .env +.eggs + +AUTHORS +ChangeLog +.venv/ diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 51fb40866eeb4e20beffb25f8f191444673454c6..0000000000000000000000000000000000000000 --- a/.travis.yml +++ /dev/null @@ -1,19 +0,0 @@ -language: python -sudo: false -python: -- '2.7' -- '3.3' -- '3.4' -- '3.5' -install: -- pip install . -- pip install nose -script: nosetests -deploy: - provider: pypi - user: vmalloc - password: - secure: dLHV+qi3Pgd61dyDDIGBO7dauuZ3hS/0guZNwkLy2g/4eSzNKcUDXDxBeYYLiduZhRPEdAkAM5pirjc0FCBJUxdrncuNOsvIphRh/DhM9aEpiaiTuGBoOyxF2UPAfJwfvIjkSbdAGjeIuTK1aCAtXkijYHmCKnAjRTRmgRgBW2g= - on: - tags: true - repo: vmalloc/pyforge diff --git a/Makefile b/Makefile index 03dc49b770365c42755606548fee2fe275628ea5..1517256e25f00c108cfba4565dce270545b4f05d 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,12 @@ default: test test: env - .env/bin/nosetests -x + .env/bin/pytest -v env: .env/.up-to-date .env/.up-to-date: setup.py Makefile - python -m virtualenv .env - .env/bin/pip install -e . - .env/bin/pip install nose + python -m venv .env + .env/bin/pip install -e ".[testing]" touch $@ diff --git a/README.rst b/README.rst index a385ca50cf85389ad94fd72817b6d4e9e7f4b0a9..44ef1fd47aec9f647042cd9db5d82e0fdedf8137 100644 --- a/README.rst +++ b/README.rst @@ -1,4 +1,4 @@ -.. image:: https://secure.travis-ci.org/vmalloc/pyforge.png +.. image:: https://github.com/vmalloc/pyforge/actions/workflows/main.yml/badge.svg?branch=master What is it? =========== @@ -7,7 +7,7 @@ Forge is a mocking library for Python. It draws most of its inspiration from Mox Running Forge's Acceptance Suite ================================ -All of Forge's acceptance tests are in the tests/ directory under the root. They require either unittest2 or the built-in unittest module (2.7/3.2 and above). +All of Forge's acceptance tests are in the tests/ directory under the root. They require the built-in unittest module. Running the tests is recommended with the *nosetests* script, but in case you don't have it, the *run_tests* script can be used instead. @@ -79,7 +79,7 @@ To promote niceness, two context managers provide syntactic sugar that structure Failures and Unexpected Events ------------------------------ -Whenever an event occurs that was not expected, an exception is raised, explaining what happend:: +Whenever an event occurs that was not expected, an exception is raised, explaining what happened:: >>> stub = forge_manager.create_function_stub(some_func) >>> stub(1, 2, 3) # doctest: +ELLIPSIS @@ -178,7 +178,7 @@ If you don't know the exact value that the argument to a function is going to ge To complete the picture, if you want to assert all sorts of checks on the arguments you are recording, you can use comparators. For instance, the following doesn't care about which argument is passed to 'name', as long as it is a string:: - my_stub(name=IsA(basestring)) + my_stub(name=IsA(str)) Many comparators exist in Forge: diff --git a/forge/__version__.py b/forge/__version__.py index 67bc602abf06e9bcea675fe21c56a2f3c76bc331..2b9fae5bbce7d2d0ed392aeed8f6748762087d96 100644 --- a/forge/__version__.py +++ b/forge/__version__.py @@ -1 +1,3 @@ -__version__ = "1.3.0" +import pkg_resources + +__version__ = pkg_resources.get_distribution('pyforge').version diff --git a/forge/class_mock_handle.py b/forge/class_mock_handle.py index 234575296f217604ea14fd673bb25ac6f69970d5..a00b60e1e587c1e2db96ecf16305e7e3a1856d86 100644 --- a/forge/class_mock_handle.py +++ b/forge/class_mock_handle.py @@ -9,7 +9,6 @@ from .exceptions import InvalidEntryPoint from .exceptions import CannotMockFunctions from .signature import FunctionSignature from .mock_handle import MockHandle -from .python3_compat import build_unbound_instance_method, IS_PY3 class ClassMockHandle(MockHandle): def __init__(self, forge, mock, mocked_class, behave_as_instance, hybrid): @@ -30,11 +29,9 @@ class ClassMockHandle(MockHandle): return hasattr(self.mocked_class, name) def _get_real_function(self, name): returned = self._get_real_method(name) - if IS_PY3: - if hasattr(returned, '__func__'): - return returned.__func__ - return returned - return returned.__func__ + if hasattr(returned, '__func__'): + return returned.__func__ + return returned def _get_real_method(self, name): if name == '__call__' and not self.behaves_as_instance: return self._get_constructor_method() @@ -46,7 +43,7 @@ class ClassMockHandle(MockHandle): # simulate an empty ctor... fake_constructor = lambda self: None fake_constructor.__name__ = "__init__" - returned = build_unbound_instance_method(fake_constructor, self.mocked_class) + returned = types.MethodType(fake_constructor, self.mocked_class) return returned def _check_unrecorded_method_getting(self, name): pass # unrecorded methods can be obtained, but not called... diff --git a/forge/comparators.py b/forge/comparators.py index ce959ea202213840d0bfd1d3c487266d7f195454..28423c186ad9bdb1420cbaca69731e29e370a578 100644 --- a/forge/comparators.py +++ b/forge/comparators.py @@ -1,5 +1,4 @@ import re -from .python3_compat import basestring class Comparator(object): def equals(self, other): @@ -38,7 +37,7 @@ class RegexpMatches(Comparator): self._regexp = regexp self._flags = flags def equals(self, other): - if not isinstance(other, basestring): + if not isinstance(other, str): return False return re.match(self._regexp, other, self._flags) def __repr__(self): @@ -50,7 +49,7 @@ class RegexpSearches(Comparator): self._regexp = regexp self._flags = flags def equals(self, other): - if not isinstance(other, basestring): + if not isinstance(other, str): return False return re.search(self._regexp, other, self._flags) def __repr__(self): @@ -125,7 +124,7 @@ class And(Comparator): def equals(self, other): return all(c.equals(other) for c in self.comparators) -StrContains = lambda *xs: And(IsA(basestring), Contains(*xs)) +StrContains = lambda *xs: And(IsA(str), Contains(*xs)) class Or(Comparator): def __init__(self, *comparators): diff --git a/forge/forge.py b/forge/forge.py index 92ab7b3334ff9a874a23d712e43ca3507d76ccba..e18e4c647cd129821047671569be2fbb7cf8b764 100644 --- a/forge/forge.py +++ b/forge/forge.py @@ -1,7 +1,6 @@ import itertools import time from contextlib import contextmanager -from .python3_compat import iteritems from .stub import FunctionStub from .queue import ForgeQueue from .class_mock import ClassMockObject @@ -87,7 +86,7 @@ class Forge(object): def create_mock_with_attrs(self, mocked_class, **attrs): returned = self.create_mock(mocked_class) - for attr, value in iteritems(attrs): + for attr, value in attrs.items(): setattr(returned, attr, value) return returned diff --git a/forge/forge_test_case.py b/forge/forge_test_case.py index 44bcb0f1aa3a3ac4542d2aea5d299a1c3c82c022..58207077732898f8ed6b3b1fe76af055bf0db541 100644 --- a/forge/forge_test_case.py +++ b/forge/forge_test_case.py @@ -1,7 +1,4 @@ -try: - from unittest2 import TestCase -except ImportError: - from unittest import TestCase +from unittest import TestCase from .forge import Forge class ForgeTestCase(TestCase): diff --git a/forge/python3_compat.py b/forge/python3_compat.py deleted file mode 100644 index 403d9755dbe05d3a109ca740891b7ab23b9cba09..0000000000000000000000000000000000000000 --- a/forge/python3_compat.py +++ /dev/null @@ -1,32 +0,0 @@ -import types -import itertools -import platform -import inspect - -IS_PY3 = (platform.python_version() >= '3') - -if IS_PY3: - iteritems = dict.items - xrange = range - basestring = str - def getargspec(x): - return inspect.getfullargspec(x)[:4] -else: - iteritems = dict.iteritems - from __builtin__ import xrange, basestring - getargspec = inspect.getargspec - -def izip(*args, **kwargs): - if IS_PY3: - return zip(*args, **kwargs) - return itertools.izip(*args, **kwargs) - -def build_instance_method(function, instance, cls): - if IS_PY3: - return types.MethodType(function, instance) - return types.MethodType(function, instance, cls) -def build_unbound_instance_method(function, cls): - if IS_PY3: - return types.MethodType(function, cls) - return types.MethodType(function, None, cls) - diff --git a/forge/sentinel.py b/forge/sentinel.py index e05e9191c8e34719a092004d975ef4acb42964d5..d643a66113b8be3c1a27cf69ad28c05aac545f04 100644 --- a/forge/sentinel.py +++ b/forge/sentinel.py @@ -1,10 +1,9 @@ -from .python3_compat import iteritems class Sentinel(object): def __init__(self, __forge__name=None, **attrs): super(Sentinel, self).__init__() self.__forge__name = __forge__name - for attr, value in iteritems(attrs): + for attr, value in attrs.items(): setattr(self, attr, value) def __repr__(self): name = self.__forge__name diff --git a/forge/signature.py b/forge/signature.py index 7d2f4e8e0fabfa82b15713fa97a9d3fc22936421..0b2504bf6b37223ee914684a347922bd31517ebe 100644 --- a/forge/signature.py +++ b/forge/signature.py @@ -1,6 +1,6 @@ +import inspect import copy import itertools -from .python3_compat import izip, iteritems, basestring, getargspec from .exceptions import SignatureException, InvalidKeywordArgument from .utils import is_bound_method from .utils import is_class_method @@ -28,12 +28,13 @@ class FunctionSignature(object): def _iter_args_and_defaults(self, args, defaults): defaults = [] if defaults is None else defaults filled_defaults = itertools.chain(itertools.repeat(NOTHING, len(args) - len(defaults)), defaults) - return izip(args, filled_defaults) + return zip(args, filled_defaults) def _build_arguments(self): self._args = [] try: - args, varargs_name, kwargs_name, defaults = getargspec(self.func)[:4] + args, varargs_name, kwargs_name, defaults = \ + inspect.getfullargspec(self.func)[:4] except TypeError: args = [] varargs_name = 'args' @@ -74,8 +75,8 @@ class FunctionSignature(object): returned[arg_name] = given_arg def _update_normalized_kwargs(self, returned, kwargs): - for arg_name, arg in iteritems(kwargs): - if not isinstance(arg_name, basestring): + for arg_name, arg in kwargs.items(): + if not isinstance(arg_name, str): raise InvalidKeywordArgument("Invalid keyword argument %r" % (arg_name,)) if arg_name in returned: raise SignatureException("%s is given more than once to %s" % (arg_name, self.func_name)) diff --git a/forge/utils.py b/forge/utils.py index 974dfd8c363e75e1a9a86ead0e07315614f2b4f9..b323e3fdaa72e37ab88cd1c8b5ab074914950cbb 100644 --- a/forge/utils.py +++ b/forge/utils.py @@ -1,8 +1,7 @@ from types import * -from .python3_compat import xrange, IS_PY3 def renumerate(collection): - for index in xrange(len(collection) - 1, -1, -1): + for index in range(len(collection) - 1, -1, -1): yield (index, collection[index]) ### object predicates @@ -11,7 +10,7 @@ def is_bound_method(obj): def is_function(obj): return isinstance(obj, FunctionType) or isinstance(obj, BuiltinFunctionType) def is_class(obj): - return isinstance(obj, type) or (not IS_PY3 and isinstance(obj, ClassType)) + return isinstance(obj, type) def is_class_method(obj): if not is_bound_method(obj): return False diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..9918bd95347de3f9915291c2d90a9ea965aabd23 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +sentinels>=0.0.4 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000000000000000000000000000000000000..4503c3c5c10c0051f0aa12a3c785653d99b43bf2 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,23 @@ +[metadata] +name = pyforge +classifiers = + Intended Audience :: Developers + License :: OSI Approved :: BSD License + Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 + Topic :: Software Development :: Libraries :: Python Modules + Topic :: Software Development :: Testing +summary = Python mocking framework +description-file = + README.rst +description-content-type = text/markdown +license = BSD +author = Rotem Yaari +author_email = vmalloc@gmail.com +url = https://github.com/vmalloc/pyforge + +[extras] +testing = + pytest diff --git a/setup.py b/setup.py index f6b55eea9ee539f7b89148a993b76b27e0906e84..7ddc1ba01e9d78c1696176454391aabd57b574d5 100644 --- a/setup.py +++ b/setup.py @@ -1,33 +1,10 @@ -import os -import platform -from setuptools import setup, find_packages +#!/usr/bin/env python +from setuptools import setup -with open(os.path.join(os.path.dirname(__file__), "forge", "__version__.py")) as version_file: - exec(version_file.read()) -_INSTALL_REQUIREMENTS = ['sentinels>=0.0.4'] -if platform.python_version_tuple() < ('2', '7'): - _INSTALL_REQUIREMENTS.append('unittest2') - -setup(name="pyforge", - classifiers = [ - "Development Status :: 4 - Beta", - "Intended Audience :: Developers", - "License :: OSI Approved :: BSD License", - "Programming Language :: Python :: 2.6", - "Programming Language :: Python :: 2.7", - "Programming Language :: Python :: 3.3", - "Programming Language :: Python :: 3.4", - "Programming Language :: Python :: 3.5", - "Topic :: Software Development :: Libraries :: Python Modules", - "Topic :: Software Development :: Testing", - ], - description="Python mocking framework", - license="BSD", - author="Rotem Yaari", - author_email="vmalloc@gmail.com", - url="http://github.com/vmalloc/pyforge", - version=__version__, - install_requires=_INSTALL_REQUIREMENTS, - packages=find_packages(exclude=["tests"]) - ) +setup( + setup_requires=['pbr>=3.0', 'setuptools>=17.1'], + pbr=True, + python_requires=">=3.6.*", + long_description_content_type='text/x-rst; charset=UTF-8', +) diff --git a/tests/test__actions.py b/tests/test__actions.py index 20ce64911d0060d2771be90d03457a54b39b6cdc..fb9a9c3da87c2a29c67de9be5c8fe27dbc9aa1e5 100644 --- a/tests/test__actions.py +++ b/tests/test__actions.py @@ -9,9 +9,9 @@ class ActionsTest(ForgeTestCase): self.stub = self.forge.create_function_stub(lambda *args, **kwargs: None) def test__return_value(self): rv = self.stub(1, 2, 3).and_return(666) - self.assertEquals(rv, 666) + self.assertEqual(rv, 666) self.forge.replay() - self.assertEquals(666, self.stub(1, 2, 3)) + self.assertEqual(666, self.stub(1, 2, 3)) self.assertNoMoreCalls() self.forge.verify() def test__raised_exception(self): @@ -30,7 +30,7 @@ class ActionsTest(ForgeTestCase): with self.assertRaises(ConflictingActions): expected_call.and_raise(Exception()) #conflict should not affect existing expectations - self.assertEquals(expected_call._return_value, 2) + self.assertEqual(expected_call._return_value, 2) self.assertIs(expected_call._raised_exception, NOTHING) expected_call = self.stub(1, 2, 3) @@ -46,34 +46,34 @@ class ActionsTest(ForgeTestCase): return_value = 666 cp = Checkpoint() rv = self.stub(1, 2, 3).and_call(cp.trigger).and_return(return_value) - self.assertEquals(rv, return_value) + self.assertEqual(rv, return_value) self.forge.replay() rv = self.stub(1, 2, 3) - self.assertEquals(rv, return_value) + self.assertEqual(rv, return_value) self.assertTrue(cp.called) def test__and_call__specify_args_kwargs(self): return_value = 666 cp = Checkpoint() def callback(a, b, c, d): - self.assertEquals((a, b, c, d), (1, 2, 3, 4)) + self.assertEqual((a, b, c, d), (1, 2, 3, 4)) cp.trigger() rv = self.stub(1, 2, 3).and_call(callback, args=(1, 2, 3), kwargs=dict(d=4)).and_return(return_value) - self.assertEquals(rv, return_value) + self.assertEqual(rv, return_value) self.forge.replay() rv = self.stub(1, 2, 3) - self.assertEquals(rv, return_value) + self.assertEqual(rv, return_value) self.assertTrue(cp.called) def test__and_call_with_args(self): return_value = 666 cp = Checkpoint() def trigger(*args, **kwargs): - self.assertEquals(args, (1, 2, 3)) - self.assertEquals(kwargs, dict(d=4)) + self.assertEqual(args, (1, 2, 3)) + self.assertEqual(kwargs, dict(d=4)) cp.trigger() rv = self.stub(1, 2, 3, d=4).and_call_with_args(trigger).and_return(return_value) - self.assertEquals(rv, return_value) + self.assertEqual(rv, return_value) self.forge.replay() rv = self.stub(1, 2, 3, d=4) - self.assertEquals(rv, return_value) + self.assertEqual(rv, return_value) self.assertTrue(cp.called) diff --git a/tests/test__class_mocks.py b/tests/test__class_mocks.py index 72c47b899c96f3e55ff4e0530542edf6aa940b26..6ff1e761f400413e1ac5b2600bee927df010334d 100644 --- a/tests/test__class_mocks.py +++ b/tests/test__class_mocks.py @@ -26,7 +26,8 @@ class _NewStyleTest(object): def _get_class_factory(Self): return build_new_style_class -class _BasicClassMockTest(_ClassMockTest): +class _BasicClassMockTest: + def _get_methods(self): return METHODS def test__isinstance(self): @@ -67,9 +68,9 @@ class _BasicClassMockTest(_ClassMockTest): with self.assertRaises(SignatureException): self.mock.regular_method(d=10) -class OldStyleBasicTest(_BasicClassMockTest, _OldStyleTest): +class OldStyleBasicTest(_ClassMockTest, _BasicClassMockTest, _OldStyleTest): pass -class NewStyleBasicTest(_BasicClassMockTest, _NewStyleTest): +class NewStyleBasicTest(_ClassMockTest, _BasicClassMockTest, _NewStyleTest): pass diff --git a/tests/test__comparators.py b/tests/test__comparators.py index 4cf0150d100bfa58e54269f34ab5e63404336908..04a0e2c7d5904cd27b6882d07860dd9362a01066 100644 --- a/tests/test__comparators.py +++ b/tests/test__comparators.py @@ -2,7 +2,6 @@ import itertools import re from .ut_utils import TestCase, BinaryObjectClass from forge.comparators import * -from forge.python3_compat import IS_PY3, basestring class Compared(object): def __eq__(self): @@ -10,7 +9,7 @@ class Compared(object): def __ne__(self): raise NotImplementedError() -class _ComparatorTest(TestCase): +class _ComparatorTest: def test__valid_equality(self): for a, b in self._get_equal_pairs(): self.assertTrue(a == b) @@ -26,10 +25,10 @@ class _ComparatorTest(TestCase): def test__representation(self): for a, _ in itertools.chain(self._get_equal_pairs(), self._get_unequal_pairs()): self.assertIsInstance(a, Comparator) - self.assertIsInstance(str(a), basestring) - self.assertEquals(str(a), repr(a)) + self.assertIsInstance(str(a), str) + self.assertEqual(str(a), repr(a)) -class IsTest(_ComparatorTest): +class IsTest(_ComparatorTest, TestCase): def _get_equal_pairs(self): c = Compared() yield Is(c), c @@ -38,20 +37,20 @@ class IsTest(_ComparatorTest): yield Is(c), Compared() yield Is(c), 2 -class IsATest(_ComparatorTest): +class IsATest(_ComparatorTest, TestCase): def _get_equal_pairs(self): c = Compared() yield IsA(Compared), c - yield IsA(basestring), "hey" + yield IsA(str), "hey" yield IsA(type(BinaryObjectClass())), BinaryObjectClass() def _get_unequal_pairs(self): yield IsA(Compared), "hey" - yield IsA(basestring), Compared() + yield IsA(str), Compared() yield IsA(type(BinaryObjectClass())), object() yield IsA(BinaryObjectClass), BinaryObjectClass yield IsA(BinaryObjectClass()), BinaryObjectClass() -class RegexpMatchesTest(_ComparatorTest): +class RegexpMatchesTest(_ComparatorTest, TestCase): def _get_equal_pairs(self): yield RegexpMatches(".+"), "hey" yield RegexpMatches(r"hey \S+"), "hey there" @@ -60,7 +59,7 @@ class RegexpMatchesTest(_ComparatorTest): yield RegexpMatches(r"hello \S+"), "hey there" yield RegexpMatches(r"hello"), 2 -class FuncTest(_ComparatorTest): +class FuncTest(_ComparatorTest, TestCase): def _get_equal_pairs(self): obj = object() yield Func(lambda o: o is obj), obj @@ -71,7 +70,7 @@ class FuncTest(_ComparatorTest): yield Func(lambda o: o is obj), other yield Func(lambda o: False), "hello" -class IsAlmostTest(_ComparatorTest): +class IsAlmostTest(_ComparatorTest, TestCase): def _get_equal_pairs(self): yield IsAlmost(3, 3), 3.0002 yield IsAlmost(3), 3.00000002 @@ -80,7 +79,7 @@ class IsAlmostTest(_ComparatorTest): yield IsAlmost(3), 3.02 yield IsAlmost(3), "hey" -class ContainsTest(_ComparatorTest): +class ContainsTest(_ComparatorTest, TestCase): def _get_equal_pairs(self): yield Contains("a"), "laugh" yield Contains("bl"), "able" @@ -97,7 +96,7 @@ class ContainsTest(_ComparatorTest): yield Contains("a", "b"), "b" yield Contains("a", "b", "d"), ["a", "b"] -class StrContainsTest(_ComparatorTest): +class StrContainsTest(_ComparatorTest, TestCase): def _get_equal_pairs(self): yield StrContains("a"), "laugh" yield StrContains("bl"), "able" @@ -112,7 +111,7 @@ class StrContainsTest(_ComparatorTest): yield StrContains("a", "b"), "a" yield StrContains("a", "b"), ["a", "b"] -class HasKeyValueTest(_ComparatorTest): +class HasKeyValueTest(_ComparatorTest, TestCase): def _get_equal_pairs(self): yield HasKeyValue('a', 1), dict(a=1, b=2) yield HasKeyValue(1, 2), [0, 2, 0, 0] @@ -123,7 +122,7 @@ class HasKeyValueTest(_ComparatorTest): yield HasKeyValue('a', 1), object() yield HasKeyValue(0, 1), [0, 0, 0] -class HasAttributeValueTest(_ComparatorTest): +class HasAttributeValueTest(_ComparatorTest, TestCase): class Object(object): a = 2 b = 3 @@ -138,7 +137,7 @@ class HasAttributeValueTest(_ComparatorTest): yield HasAttributeValue('bla', 2), Object() yield HasAttributeValue('bloop', 2), dict(bloop=2) -class AnythingTest(_ComparatorTest): +class AnythingTest(_ComparatorTest, TestCase): def _get_equal_pairs(self): yield Anything(), object yield Anything(), object() @@ -148,30 +147,30 @@ class AnythingTest(_ComparatorTest): def _get_unequal_pairs(self): return () -class AndTest(_ComparatorTest): +class AndTest(_ComparatorTest, TestCase): def _get_equal_pairs(self): - yield And(IsA(basestring), Contains('a')), "Boa" - yield And(IsA(basestring), Contains('a'), Contains('g')), "Bga" + yield And(IsA(str), Contains('a')), "Boa" + yield And(IsA(str), Contains('a'), Contains('g')), "Bga" yield And(IsA(int)), 2 def _get_unequal_pairs(self): - yield And(IsA(basestring), Contains('a')), 2 - yield And(IsA(basestring), Contains('a'), Contains('g')), "Boa" + yield And(IsA(str), Contains('a')), 2 + yield And(IsA(str), Contains('a'), Contains('g')), "Boa" yield And(IsA(int)), "a" def test__empty_and(self): with self.assertRaises(TypeError): And() -class OrTest(_ComparatorTest): +class OrTest(_ComparatorTest, TestCase): def _get_equal_pairs(self): - yield Or(IsA(basestring), IsA(int)), "a" - yield Or(Anything(), IsA(basestring)), 2 - yield Or(IsA(basestring), Anything()), 2 + yield Or(IsA(str), IsA(int)), "a" + yield Or(Anything(), IsA(str)), 2 + yield Or(IsA(str), Anything()), 2 def _get_unequal_pairs(self): - yield Or(IsA(basestring), IsA(int)), 2.0 + yield Or(IsA(str), IsA(int)), 2.0 def test__empty_or(self): with self.assertRaises(TypeError): Or() -class NotTest(_ComparatorTest): +class NotTest(_ComparatorTest, TestCase): def _get_equal_pairs(self): yield Not(IsA(int)), "a" def _get_unequal_pairs(self): diff --git a/tests/test__context_managers.py b/tests/test__context_managers.py index 485635a43404c8a3af964f6122fd2e0aab901e17..15fcd48f43e65145a0e196a05fb21079041c7f90 100644 --- a/tests/test__context_managers.py +++ b/tests/test__context_managers.py @@ -2,7 +2,6 @@ import types from .ut_utils import ForgeTestCase from forge import UnexpectedCall from forge import UnexpectedSetattr -from forge.python3_compat import IS_PY3 class ContextManager(object): def __enter__(self): @@ -34,14 +33,14 @@ class ContextManagerTest(ForgeTestCase): with self.obj: raise my_exception caught = caught.exception - self.assertEquals(len(caught.expected), 1) + self.assertEqual(len(caught.expected), 1) self.assertIs(caught.expected[0].target, self.checkpoint) self.assertIsSameMethod(caught.got.target.__forge__.original, ContextManager.__exit__) self.assertIs(caught.got.args['t'], Exception) self.assertIs(caught.got.args['v'], my_exception) self.assertIsNotNone(caught.got.args['tb']) - self.assertEquals(len(self.forge.queue), 2) + self.assertEqual(len(self.forge.queue), 2) self.forge.reset() def test__expecting_context_with_unexpected_call_inside(self): stub = self.forge.create_function_stub(lambda arg: None) @@ -52,12 +51,12 @@ class ContextManagerTest(ForgeTestCase): with self.obj: stub(2) caught = caught.exception - self.assertEquals(len(caught.expected), 1) + self.assertEqual(len(caught.expected), 1) self.assertIs(caught.expected[0].target, stub) self.assertIs(caught.got.target, stub) self.assertIs(caught.expected[0].args['arg'], 1) self.assertIs(caught.got.args['arg'], 2) - self.assertEquals(len(self.forge.queue), 2) + self.assertEqual(len(self.forge.queue), 2) self.forge.reset() def test__expecting_context_with_unexpected_setattr_inside(self): with self.obj: @@ -67,14 +66,14 @@ class ContextManagerTest(ForgeTestCase): with self.obj: self.obj.a = 2 caught = caught.exception - self.assertEquals(len(caught.expected), 1) + self.assertEqual(len(caught.expected), 1) self.assertIs(caught.expected[0].target, self.obj.__forge__.get_attribute('__exit__')) - self.assertEquals(len(caught.expected[0].args), 3) + self.assertEqual(len(caught.expected[0].args), 3) self.assertTrue(all(x is None for x in caught.expected[0].args.values())) self.assertIs(caught.got.target, self.obj) - self.assertEquals(caught.got.name, 'a') - self.assertEquals(caught.got.value, 2) - self.assertEquals(len(self.forge.queue), 1) + self.assertEqual(caught.got.name, 'a') + self.assertEqual(caught.got.value, 2) + self.assertEqual(len(self.forge.queue), 1) self.forge.reset() def test__enter_returning_value(self): self.obj.__enter__().and_return(2) @@ -83,12 +82,10 @@ class ContextManagerTest(ForgeTestCase): self.forge.replay() with self.obj as value: self.checkpoint() - self.assertEquals(value, 2) + self.assertEqual(value, 2) def assertIsSameMethod(self, a, b): - if IS_PY3: - if not isinstance(a, types.FunctionType): - a = a.__func__ - if not isinstance(b, types.FunctionType): - b = b.__func__ - return a is b - return a.__func__ is b.__func__ + if not isinstance(a, types.FunctionType): + a = a.__func__ + if not isinstance(b, types.FunctionType): + b = b.__func__ + return a is b diff --git a/tests/test__function_stubs.py b/tests/test__function_stubs.py index d8e5fcaf604a178a0976d5bba8222efbf1e5484f..af47722056e97074c94a62d9ebc4f4b5b8bfc3a3 100644 --- a/tests/test__function_stubs.py +++ b/tests/test__function_stubs.py @@ -2,12 +2,8 @@ from numbers import Number from .ut_utils import ForgeTestCase from forge.stub import FunctionStub from forge.stub_handle import StubHandle -from forge.python3_compat import IS_PY3 -if IS_PY3: - from urllib.request import urlopen -else: - from urllib2 import urlopen +from urllib.request import urlopen def some_function(): "some doc" @@ -19,19 +15,19 @@ class FunctionStubbingTest(ForgeTestCase): urlopen_stub = self.forge.create_function_stub(urlopen) urlopen_stub("url").and_return(666) self.forge.replay() - self.assertEquals(urlopen_stub("url"), 666) + self.assertEqual(urlopen_stub("url"), 666) class FunctionStubAttributesTest(ForgeTestCase): def setUp(self): super(FunctionStubAttributesTest, self).setUp() self.stub = self.forge.create_function_stub(some_function) def test__name(self): - self.assertEquals(self.stub.__name__, some_function.__name__) + self.assertEqual(self.stub.__name__, some_function.__name__) def test__specific_name(self): stub = self.forge.create_function_stub(some_function, name='other_name') - self.assertEquals(stub.__name__, 'other_name') + self.assertEqual(stub.__name__, 'other_name') def test__doc(self): - self.assertEquals(self.stub.__doc__, some_function.__doc__) + self.assertEqual(self.stub.__doc__, some_function.__doc__) def test__stub_id(self): self.assertIsInstance(self.stub.__forge__.id, Number) def test__str_repr(self): diff --git a/tests/test__hybrid_mock.py b/tests/test__hybrid_mock.py index 1de3544b008dfa017aa90ed23c7d8cebdd08069a..e22ed8cd6b177a97e8198f70aa932f5a6ef79491 100644 --- a/tests/test__hybrid_mock.py +++ b/tests/test__hybrid_mock.py @@ -67,7 +67,7 @@ class HybridMockTest(ForgeTestCase): hm.__forge__.enable_setattr_during_replay() self.forge.replay() hm.set_value() - self.assertEquals(hm.value, 2) + self.assertEqual(hm.value, 2) class ClassWithClassmethodConstructor(object): def __init__(self, a, b, c): diff --git a/tests/test__interleaved.py b/tests/test__interleaved.py index c758d8d29e9ab7a68d741c2d297e39a74d245018..455f5ac7e14e5371b626c0b5209ada0047db0078 100644 --- a/tests/test__interleaved.py +++ b/tests/test__interleaved.py @@ -24,10 +24,10 @@ class InterleavedTest(ForgeTestCase): self.obj.f(2, 3, 4).and_return(3) self.forge.replay() - self.assertEquals(self.obj.f(1, 2, 3), 2) + self.assertEqual(self.obj.f(1, 2, 3), 2) with self.assertRaises(UnexpectedCall): self.obj.f(1, 2, 4) - self.assertEquals(self.obj.f(2, 3, 4), 3) + self.assertEqual(self.obj.f(2, 3, 4), 3) def test__interleaved__two_ordered_nested(self): with self.forge.interleaved_order(): @@ -58,7 +58,7 @@ class InterleavedTest(ForgeTestCase): self.obj.f(*thread[1][0]) args, ret = thread.pop(0) - self.assertEquals(self.obj.f(*args), ret) + self.assertEqual(self.obj.f(*args), ret) if not thread: parallels.pop(0) diff --git a/tests/test__mock_attributes.py b/tests/test__mock_attributes.py index 228fc02759b6084c4584d8851b10c4414c9cd463..45c13cf47058e3e137d72b499bde2c3b1c202363 100644 --- a/tests/test__mock_attributes.py +++ b/tests/test__mock_attributes.py @@ -22,7 +22,7 @@ class MockAttributesTest(ForgeTestCase): self.forge.replay() self.assertIs(self.obj.a, attr_value) self.forge.reset() - self.assertEquals(self.obj.a, attr_value) + self.assertEqual(self.obj.a, attr_value) def test__setting_mock_object_attributes_during_replay(self): self.forge.replay() with self.assertUnexpectedSetattr(self.obj, "a", 2): @@ -36,7 +36,7 @@ class MockAttributesTest(ForgeTestCase): self.obj.__forge__.enable_setattr_during_replay() self.forge.replay() self.obj.a = 2 - self.assertEquals(self.obj.a, 2) + self.assertEqual(self.obj.a, 2) self.forge.reset() with self.assertRaises(AttributeError): self.obj.a @@ -52,9 +52,9 @@ class MockAttributesTest(ForgeTestCase): with self.assertUnexpectedSetattr(self.obj, "a", 3): self.obj.a = 3 self.assertNoMoreCalls() - self.assertEquals(self.obj.a, 2) + self.assertEqual(self.obj.a, 2) self.forge.reset() - self.assertEquals(self.obj.a, 2) + self.assertEqual(self.obj.a, 2) def test__getattr_of_nonexisting_attr_during_replay(self): self.forge.replay() with self.assertRaises(AttributeError): @@ -67,15 +67,15 @@ class MockAttributesTest(ForgeTestCase): self.forge.replay() self.obj.some_method def test__getattr_of_class_variables_during_record(self): - self.assertEquals(self.obj.class_variable, MockedClass.class_variable) + self.assertEqual(self.obj.class_variable, MockedClass.class_variable) def test__getattr_of_class_variables_during_replay(self): self.forge.replay() - self.assertEquals(self.obj.class_variable, MockedClass.class_variable) + self.assertEqual(self.obj.class_variable, MockedClass.class_variable) def test__setattr_of_class_variables_during_record(self): self.obj.class_variable = 300 - self.assertEquals(self.obj.class_variable, 300) + self.assertEqual(self.obj.class_variable, 300) self.forge.replay() - self.assertEquals(self.obj.class_variable, 300) + self.assertEqual(self.obj.class_variable, 300) def test__setattr_of_class_variables_during_replay(self): self.forge.replay() with self.assertUnexpectedSetattr(self.obj, "class_variable", 300): @@ -86,28 +86,28 @@ class MockAttributesTest(ForgeTestCase): with self.assertRaises(AttributeError): self.obj.a self.obj.a = 666 - self.assertEquals(self.obj.a, 666) + self.assertEqual(self.obj.a, 666) def test__expect_setattr_previous_value(self): self.obj.a = 1 - self.assertEquals(self.obj.a, 1) + self.assertEqual(self.obj.a, 1) self.obj.__forge__.expect_setattr("a", 2) - self.assertEquals(self.obj.a, 1) + self.assertEqual(self.obj.a, 1) self.forge.replay() - self.assertEquals(self.obj.a, 1) + self.assertEqual(self.obj.a, 1) self.obj.a = 2 - self.assertEquals(self.obj.a, 2) + self.assertEqual(self.obj.a, 2) self.assertNoMoreCalls() self.forge.reset() - self.assertEquals(self.obj.a, 1) + self.assertEqual(self.obj.a, 1) def test__expect_setattr_not_happening(self): self.obj.__forge__.expect_setattr("a", 666) self.forge.replay() with self.assertRaises(ExpectedEventsNotFound) as caught: self.forge.verify() - self.assertEquals(len(caught.exception.events), 1) + self.assertEqual(len(caught.exception.events), 1) self.assertIs(caught.exception.events[0].target, self.obj) - self.assertEquals(caught.exception.events[0].name, "a") - self.assertEquals(caught.exception.events[0].value, 666) + self.assertEqual(caught.exception.events[0].name, "a") + self.assertEqual(caught.exception.events[0].value, 666) self.forge.reset() @contextmanager def assertUnexpectedSetattr(self, target, name, value): @@ -115,8 +115,8 @@ class MockAttributesTest(ForgeTestCase): yield None exception = caught.exception self.assertIs(exception.got.target, target) - self.assertEquals(exception.got.name, name) - self.assertEquals(exception.got.value, value) + self.assertEqual(exception.got.name, name) + self.assertEqual(exception.got.value, value) diff --git a/tests/test__mocking_instances.py b/tests/test__mocking_instances.py index 35251ab024759c78777d4052f9df8a6e1f1a4f28..0df0d3f36e82f77fe6347fad87a612c7970638c3 100644 --- a/tests/test__mocking_instances.py +++ b/tests/test__mocking_instances.py @@ -16,7 +16,7 @@ class MockingTest(ForgeTestCase): def test__mock_hashability(self): self._assert_mock_not_hashable(self.obj) self.obj.__forge__.enable_hashing() - self.assertEquals(id(self.obj), hash(self.obj)) + assert hash(self.obj) self.obj.__forge__.disable_hashing() self._assert_mock_not_hashable(self.obj) def _assert_mock_not_hashable(self, obj): @@ -71,8 +71,8 @@ class MockingCornerCasesTest(ForgeTestCase): with self.assertRaises(AttributeError): mock.gettt self.forge.replay() - self.assertEquals(mock.get(2, 3), 4) - self.assertEquals(mock[6], 7) + self.assertEqual(mock.get(2, 3), 4) + self.assertEqual(mock[6], 7) class InvalidMockingTest(ForgeTestCase): def test__cannot_mock_functions(self): diff --git a/tests/test__readme.py b/tests/test__readme.py index 586a6a9e478ff7af96c2275416006e77955e5a20..3aff0daab462e007b848dea36784f5f133c18530 100644 --- a/tests/test__readme.py +++ b/tests/test__readme.py @@ -7,4 +7,4 @@ class ReadmeTest(TestCase): readme_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "README.rst")) self.assertTrue(os.path.exists(readme_path)) result = doctest.testfile(readme_path, module_relative=False) - self.assertEquals(result.failed, 0, "%s tests failed!" % result.failed) + self.assertEqual(result.failed, 0, "%s tests failed!" % result.failed) diff --git a/tests/test__record_replay.py b/tests/test__record_replay.py index df43f27791b97af23cc8bf1a4c16d1f729c466f6..c06080294e16c7293fb47391123b8c9c3c9b12b9 100644 --- a/tests/test__record_replay.py +++ b/tests/test__record_replay.py @@ -21,15 +21,15 @@ class RecordReplayTest(ForgeTestCase): for i in range(2): self.stub(1, 2, 3) self.stub(1, 2, 3) - self.assertEquals(self.stub.__forge__.call_count, 0) + self.assertEqual(self.stub.__forge__.call_count, 0) self.forge.replay() self.stub(1, 2, 3) - self.assertEquals(self.stub.__forge__.call_count, 1) + self.assertEqual(self.stub.__forge__.call_count, 1) self.stub(1, 2, 3) - self.assertEquals(self.stub.__forge__.call_count, 2) + self.assertEqual(self.stub.__forge__.call_count, 2) self.assertIn("2 times", str(self.stub)) self.forge.reset() - self.assertEquals(self.stub.__forge__.call_count, 0) + self.assertEqual(self.stub.__forge__.call_count, 0) def test__record_replay_different_not_equal_value(self): self.stub(1, 2, 3) @@ -37,9 +37,9 @@ class RecordReplayTest(ForgeTestCase): with self.assertRaises(UnexpectedCall) as caught: self.stub(1, 2, 6) exc = caught.exception - self.assertEquals(len(exc.expected), 1) - self.assertEquals(exc.expected[0].args, dict(a=1, b=2, c=3)) - self.assertEquals(exc.got.args, dict(a=1, b=2, c=6)) + self.assertEqual(len(exc.expected), 1) + self.assertEqual(exc.expected[0].args, dict(a=1, b=2, c=3)) + self.assertEqual(exc.got.args, dict(a=1, b=2, c=6)) self.assertExpectedNotMet([self.stub]) def test__record_replay_different_more_args(self): self.stub(1, 2, 3) @@ -64,7 +64,7 @@ class RecordReplayTest(ForgeTestCase): self.stub(1, 2, 3) with self.assertRaises(UnexpectedCall) as caught: self.stub(1, 2, 3) - self.assertEquals(caught.exception.expected, []) + self.assertEqual(caught.exception.expected, []) self.assertIs(caught.exception.got.target, self.stub) self.assertNoMoreCalls() self.forge.verify() @@ -96,11 +96,11 @@ class RecordReplayTest(ForgeTestCase): self.assertGreater(len(stubs), 0) with self.assertRaises(ExpectedEventsNotFound) as caught: self.forge.verify() - self.assertEquals(len(caught.exception.events), len(stubs)) + self.assertEqual(len(caught.exception.events), len(stubs)) expected = self.forge.queue.get_expected() for stub, exception_call in zip(stubs, caught.exception.events): expected_call = expected.pop() self.assertIs(expected_call, exception_call) self.assertIs(expected_call.target, stub) - self.assertEquals(len(expected), 0) + self.assertEqual(len(expected), 0) self.forge.queue.clear() diff --git a/tests/test__record_replay_ordering.py b/tests/test__record_replay_ordering.py index b483a3e858848cb3f71a8a6bb7a5e34061b3b53b..c218a04f046ac78148a474c8231c6b1273079811 100644 --- a/tests/test__record_replay_ordering.py +++ b/tests/test__record_replay_ordering.py @@ -1,6 +1,5 @@ from .ut_utils import ForgeTestCase from forge import UnexpectedCall, ExpectedEventsNotFound -from forge.python3_compat import basestring import random class OrderingTest(ForgeTestCase): @@ -15,7 +14,7 @@ class StrictOrderingTest(OrderingTest): with self.assertRaises(UnexpectedCall) as caught: self.stub(2) self.assertIs(caught.exception.got.target, self.stub) - self.assertEquals(len(caught.exception.expected), 1) + self.assertEqual(len(caught.exception.expected), 1) self.assertIs(caught.exception.expected[0].target, self.stub) self.forge.reset() @@ -103,8 +102,8 @@ class OrderingGroupExceptionFormatting(OrderingTest): with self.assertRaises(UnexpectedCall) as caught: self.stub(4) self.assertIsInstance(caught.exception.expected, list) - self.assertIsInstance(str(caught.exception), basestring) - self.assertIsInstance(repr(caught.exception), basestring) + self.assertIsInstance(str(caught.exception), str) + self.assertIsInstance(repr(caught.exception), str) self.forge.reset() @@ -132,8 +131,8 @@ class OrderedOrderedGroupTest(OrderingTest): with self.assertRaises(UnexpectedCall) as caught: self.stub(2) self.assertIsInstance(caught.exception.expected, list) - self.assertIsInstance(str(caught.exception), basestring) - self.assertIsInstance(repr(caught.exception), basestring) + self.assertIsInstance(str(caught.exception), str) + self.assertIsInstance(repr(caught.exception), str) self.forge.reset() @@ -177,8 +176,8 @@ class OrderedAnyOrderGroupTest(OrderingTest): with self.assertRaises(UnexpectedCall) as caught: self.stub(2) self.assertIsInstance(caught.exception.expected, list) - self.assertIsInstance(str(caught.exception), basestring) - self.assertIsInstance(repr(caught.exception), basestring) + self.assertIsInstance(str(caught.exception), str) + self.assertIsInstance(repr(caught.exception), str) self.forge.reset() @@ -215,8 +214,8 @@ class OrderedAnyOrder2GroupTest(OrderingTest): with self.assertRaises(UnexpectedCall) as caught: self.stub('b') self.assertIsInstance(caught.exception.expected, list) - self.assertIsInstance(str(caught.exception), basestring) - self.assertIsInstance(repr(caught.exception), basestring) + self.assertIsInstance(str(caught.exception), str) + self.assertIsInstance(repr(caught.exception), str) self.forge.reset() class OrderedAnyOrderOrderedGroupTest(OrderingTest): diff --git a/tests/test__replacing.py b/tests/test__replacing.py index 27c237f03c3eadab7bed9bb4add87ab24b94934d..848c9ea6d4a29c4799650bee25acc22f81d542ef 100644 --- a/tests/test__replacing.py +++ b/tests/test__replacing.py @@ -4,7 +4,6 @@ import types from .ut_utils import ForgeTestCase, BinaryObjectClass from forge.stub import FunctionStub from forge.class_mock import ClassMockObject -from forge.python3_compat import IS_PY3 orig_time_sleep = time.sleep orig_os_path_join = os.path.join @@ -100,7 +99,7 @@ class StubbingModulesTest(ForgeTestCase): expected_result = 666 time.sleep(10).and_return(expected_result) self.forge.replay() - self.assertEquals(time.sleep(10), expected_result) + self.assertEqual(time.sleep(10), expected_result) self.forge.restore_all_replacements() self.assertIs(time.sleep, orig_time_sleep) def test__stub_module_functions(self): @@ -111,7 +110,7 @@ class StubbingModulesTest(ForgeTestCase): return_path = "return_path" os.path.join("a", "b", "c").and_return(return_path) self.forge.replay() - self.assertEquals(return_path, os.path.join("a", "b", "c")) + self.assertEqual(return_path, os.path.join("a", "b", "c")) self.forge.verify() self.forge.restore_all_replacements() self.assertIs(os.path.join, orig_os_path_join) @@ -121,17 +120,17 @@ class ReplacingTest(ForgeTestCase): s = self.forge.create_sentinel() s.a = 2 self.forge.replace_with(s, "a", 3) - self.assertEquals(s.a, 3) + self.assertEqual(s.a, 3) self.forge.restore_all_replacements() - self.assertEquals(s.a, 2) + self.assertEqual(s.a, 2) def test__replacing_properties__new_style(self): self._test__replacing_properties(NewStyleClass, orig_newstyle_property) def test__replacing_properties__old_style(self): self._test__replacing_properties(OldStyleClass, orig_oldstyle_property) def _test__replacing_properties(self, cls, orig): self.forge.replace_with(cls, "some_property", 3) - self.assertEquals(cls.some_property, 3) - self.assertEquals(cls().some_property, 3) + self.assertEqual(cls.some_property, 3) + self.assertEqual(cls().some_property, 3) self.forge.restore_all_replacements() self.assertIs(cls.some_property, orig) self.assertIs(cls().some_property, 2) @@ -189,7 +188,7 @@ class MultipleStubbingTest(ForgeTestCase): self.forge.replay() returned = self.forge.replace_many(some_object, "a", "b", "c") - self.assertEquals(returned, expected_results) + self.assertEqual(returned, expected_results) self.forge.restore_all_replacements() self.forge.verify() self.assertNoMoreCalls() diff --git a/tests/test__sentinels.py b/tests/test__sentinels.py index 043e1e5a715f69df771e3a22e2f033d02bc7e1e6..0e670af891caa8d99054e67281abb3db85ef1619 100644 --- a/tests/test__sentinels.py +++ b/tests/test__sentinels.py @@ -13,8 +13,8 @@ class SentinelTest(ForgeTestCase): self.assertIn('s1', repr(s1)) def test__sentinal_attrs(self): s = self.forge.create_sentinel('name1', a=2, b=3, name='name2') - self.assertEquals(s.a, 2) - self.assertEquals(s.b, 3) + self.assertEqual(s.a, 2) + self.assertEqual(s.b, 3) for s in (str(s), repr(s)): self.assertIn('name1', s) self.assertNotIn('name2', s) diff --git a/tests/test__signature_object.py b/tests/test__signature_object.py index c91d386a8375efa79a101087053c94d7f2d801cf..304b10440cfb5ab7a9fac26ee7b5d71d0caf7bba 100644 --- a/tests/test__signature_object.py +++ b/tests/test__signature_object.py @@ -12,7 +12,7 @@ class ExpectedArg(object): class SignatureTest(TestCase): def _assert_argument_names(self, sig, names): - self.assertEquals([arg.name for arg in sig._args], names) + self.assertEqual([arg.name for arg in sig._args], names) def _test_function_signature(self, func, @@ -22,19 +22,19 @@ class SignatureTest(TestCase): ): sig = FunctionSignature(func) - self.assertEquals(len(expected_signature), len(sig._args)) - self.assertEquals(len(expected_signature), sig.get_num_args()) + self.assertEqual(len(expected_signature), len(sig._args)) + self.assertEqual(len(expected_signature), sig.get_num_args()) for expected_arg, arg in zip(expected_signature, sig._args): if isinstance(expected_arg, tuple): expected_arg = ExpectedArg(*expected_arg) - self.assertEquals(expected_arg.name, + self.assertEqual(expected_arg.name, arg.name) - self.assertEquals(expected_arg.has_default, + self.assertEqual(expected_arg.has_default, arg.has_default()) if expected_arg.has_default: - self.assertEquals(expected_arg.default, arg.default) - self.assertEquals(sig.has_variable_kwargs(), has_varkwargs) - self.assertEquals(sig.has_variable_args(), has_varargs) + self.assertEqual(expected_arg.default, arg.default) + self.assertEqual(sig.has_variable_kwargs(), has_varkwargs) + self.assertEqual(sig.has_variable_args(), has_varargs) def test__simple_functions(self): def f(a, b, c): @@ -128,7 +128,7 @@ class SignatureTest(TestCase): class BinaryFunctionSignatureTest(TestCase): def test__binary_global_function(self): sig = FunctionSignature(time.time) - self.assertEquals(sig._args, []) + self.assertEqual(sig._args, []) self.assertTrue(sig.has_variable_args()) self.assertTrue(sig.has_variable_kwargs()) def test__object_method_placeholders(self): diff --git a/tests/test__special_methods.py b/tests/test__special_methods.py index 28a9d25c928d81f02b9c4b09a27b1a5cb1f49e78..2720ee5780bc3e6753ae7e67b3da5ec229ef2995 100644 --- a/tests/test__special_methods.py +++ b/tests/test__special_methods.py @@ -5,9 +5,8 @@ from .ut_utils import build_new_style_class from .ut_utils import build_old_style_class from .ut_utils import Method from forge import UnexpectedCall -from forge.python3_compat import IS_PY3 -class _SpecialMethodsTest(ForgeTestCase): +class _SpecialMethodsTest: def setUp(self): super(_SpecialMethodsTest, self).setUp() self.obj = self.forge.create_mock(self.CTOR([ @@ -28,7 +27,7 @@ class _SpecialMethodsTest(ForgeTestCase): def test__len(self): self.obj.__len__().and_return(2) self.forge.replay() - self.assertEquals(len(self.obj), 2) + self.assertEqual(len(self.obj), 2) def test__setitem_explicit(self): self.obj.__setitem__('a', 'b') self.forge.replay() @@ -42,22 +41,22 @@ class _SpecialMethodsTest(ForgeTestCase): self.forge.replay() with self.assertRaises(UnexpectedCall): self.obj['a'] = 'c' - self.assertEquals(len(self.forge.queue), 1) + self.assertEqual(len(self.forge.queue), 1) self.forge.reset() def test__getitem_explicit(self): self.obj.__getitem__(2).and_return(3) self.forge.replay() - self.assertEquals(self.obj[2], 3) + self.assertEqual(self.obj[2], 3) def test__getitem_implicit(self): self.obj[2].and_return(3) self.forge.replay() - self.assertEquals(self.obj[2], 3) + self.assertEqual(self.obj[2], 3) def test__getitem_mismatch(self): self.obj[2].and_return(3) self.forge.replay() with self.assertRaises(UnexpectedCall): self.obj[3] - self.assertEquals(len(self.forge.queue), 1) + self.assertEqual(len(self.forge.queue), 1) self.forge.reset() def test__delitem_explicit(self): self.obj.__delitem__(2) @@ -72,24 +71,24 @@ class _SpecialMethodsTest(ForgeTestCase): self.forge.replay() with self.assertRaises(UnexpectedCall): del self.obj[3] - self.assertEquals(len(self.forge.queue), 1) + self.assertEqual(len(self.forge.queue), 1) self.forge.reset() def test__iter(self): expected_result = [1, 3, 4, 5] self.obj.__iter__().and_return(iter(expected_result)) self.forge.replay() l = [x for x in self.obj] - self.assertEquals(l, expected_result) + self.assertEqual(l, expected_result) def test__call(self): self.obj(1, 2, c=3).and_return(5) self.forge.replay() - self.assertEquals(self.obj(1, 2, c=3), 5) + self.assertEqual(self.obj(1, 2, c=3), 5) def test__call_mismatch(self): self.obj(1, 2, c=3).and_return(5) self.forge.replay() with self.assertRaises(UnexpectedCall): self.obj(1, 2, c=4) - self.assertEquals(len(self.forge.queue), 1) + self.assertEqual(len(self.forge.queue), 1) self.forge.reset() def test__contains_explicit(self): self.obj.__contains__(2).and_return(True) @@ -102,23 +101,20 @@ class _SpecialMethodsTest(ForgeTestCase): self.forge.replay() with self.assertRaises(UnexpectedCall): 3 in self.obj - self.assertEquals(len(self.forge.queue), 1) + self.assertEqual(len(self.forge.queue), 1) self.forge.reset() def test__boolean(self): - if IS_PY3: - self.obj.__bool__().and_return(False) - else: - self.obj.__nonzero__().and_return(False) + self.obj.__bool__().and_return(False) self.forge.replay() self.assertFalse(self.obj) -class NewStyleSpecialMethodsTest(_SpecialMethodsTest): +class NewStyleSpecialMethodsTest(_SpecialMethodsTest, ForgeTestCase): CTOR = staticmethod(build_new_style_class) -class OldStyleSpecialMethodsTest(_SpecialMethodsTest): +class OldStyleSpecialMethodsTest(_SpecialMethodsTest, ForgeTestCase): CTOR = staticmethod(build_old_style_class) -class _SpecialMethodAbsenceTest(ForgeTestCase): +class _SpecialMethodAbsenceTest: def tearDown(self): self.forge.verify() self.assertNoMoreCalls() @@ -148,11 +144,11 @@ class _SpecialMethodAbsenceTest(ForgeTestCase): '3 in self.obj' ] -class NewStyleSpecialMethodsAbsenceTest(_SpecialMethodAbsenceTest): +class NewStyleSpecialMethodsAbsenceTest(_SpecialMethodAbsenceTest, ForgeTestCase): def setUp(self): super(NewStyleSpecialMethodsAbsenceTest, self).setUp() self.obj = self.forge.create_mock(build_new_style_class()) -class OldStyleSpecialMethodsAbsenceTest(_SpecialMethodAbsenceTest): +class OldStyleSpecialMethodsAbsenceTest(_SpecialMethodAbsenceTest, ForgeTestCase): def setUp(self): super(OldStyleSpecialMethodsAbsenceTest, self).setUp() self.obj = self.forge.create_mock(build_old_style_class()) @@ -175,4 +171,3 @@ class CallCornerCasesTest(ForgeTestCase): obj(1, 2, 3) self.forge.replay() obj(1, 2, 3) - diff --git a/tests/test__utils.py b/tests/test__utils.py index fe443b387398a936c0117a6664a65c6f0cf15f30..8ed59ce9f4cbbd441d961b508f5684e5cf79e07f 100644 --- a/tests/test__utils.py +++ b/tests/test__utils.py @@ -4,7 +4,7 @@ from forge.class_mock_handle import ClassMockHandle class RenumerateTest(TestCase): def test__simple_usage(self): - self.assertEquals(list(renumerate(range(5))), + self.assertEqual(list(renumerate(range(5))), [(4, 4), (3, 3), (2, 2), (1, 1), (0, 0)]) class EXPECTING_Test(ForgeTestCase): diff --git a/tests/test__whenever.py b/tests/test__whenever.py index 926772791962724257cade8809a3fb2f5c91ffc3..f5de6eafe8e9525b996395bab94cd1d659ee7311 100644 --- a/tests/test__whenever.py +++ b/tests/test__whenever.py @@ -15,10 +15,10 @@ class WheneverTest(ForgeTestCase): def test__whenever_verifies_arguments(self): self.obj.f(1, 2, 3).whenever().and_return(2) self.forge.replay() - self.assertEquals(self.obj.f(1, 2, 3), 2) + self.assertEqual(self.obj.f(1, 2, 3), 2) with self.assertRaises(UnexpectedCall): self.obj.f(1, 2, 4) - self.assertEquals(self.obj.f(1, 2, 3), 2) + self.assertEqual(self.obj.f(1, 2, 3), 2) def test__whenever_accepts_zero_times(self): self.obj.f(1, 2, 3).whenever().and_return(2) self.forge.replay() @@ -38,12 +38,12 @@ class WheneverTest(ForgeTestCase): call.whenever().and_return("hey") self.forge.replay() for i in range(3): - self.assertEquals("hey", self.obj.f(1, 2, 3)) - self.assertEquals(1, self.obj.f(1, 1, 1)) - self.assertEquals("hey", self.obj.f(1, 2, 3)) - self.assertEquals(2, self.obj.f(2, 2, 2)) - self.assertEquals(3, self.obj.f(3, 3, 3)) - self.assertEquals("hey", self.obj.f(1, 2, 3)) + self.assertEqual("hey", self.obj.f(1, 2, 3)) + self.assertEqual(1, self.obj.f(1, 1, 1)) + self.assertEqual("hey", self.obj.f(1, 2, 3)) + self.assertEqual(2, self.obj.f(2, 2, 2)) + self.assertEqual(3, self.obj.f(3, 3, 3)) + self.assertEqual("hey", self.obj.f(1, 2, 3)) def test__whenever_patterns(self): values = list(range(10)) for value in values: @@ -52,15 +52,15 @@ class WheneverTest(ForgeTestCase): for i in range(3): random.shuffle(values) for value in values: - self.assertEquals(self.obj.g(value), value * 2) + self.assertEqual(self.obj.g(value), value * 2) with self.assertRaises(UnexpectedCall): self.obj.g(max(values)+1) def test__whenever_when_syntax(self): self.obj.g.when(1).then_return(2) self.forge.replay() - self.assertEquals(self.obj.g(1), 2) - self.assertEquals(self.obj.g(1), 2) - self.assertEquals(self.obj.g(1), 2) + self.assertEqual(self.obj.g(1), 2) + self.assertEqual(self.obj.g(1), 2) + self.assertEqual(self.obj.g(1), 2) with self.assertRaises(UnexpectedCall): self.obj.g(2) def test__whenever_when_syntax_disabled_in_replay(self): @@ -76,13 +76,13 @@ class WheneverTest(ForgeTestCase): self.obj.f(3, 2, 1).and_return(24) self.obj.g.when(1).then_return(3) self.forge.replay() - self.assertEquals(self.obj.g(1), 2) - self.assertEquals(self.obj.g(1), 2) - self.assertEquals(self.obj.f(1, 2, 3), 42) + self.assertEqual(self.obj.g(1), 2) + self.assertEqual(self.obj.g(1), 2) + self.assertEqual(self.obj.f(1, 2, 3), 42) - self.assertEquals(self.obj.g(1), 3) - self.assertEquals(self.obj.g(1), 3) - self.assertEquals(self.obj.f(3, 2, 1), 24) + self.assertEqual(self.obj.g(1), 3) + self.assertEqual(self.obj.g(1), 3) + self.assertEqual(self.obj.f(3, 2, 1), 24) def test__whenever_applies_only_in_group(self): result = object() with self.forge.ordered(): diff --git a/tests/test__wildcards.py b/tests/test__wildcards.py index 6ff7a712026d97e04f998f39d03765596a9ee7c3..ecf25ce13e0cead403d9630f00f616ee9e5089e1 100644 --- a/tests/test__wildcards.py +++ b/tests/test__wildcards.py @@ -1,6 +1,5 @@ from .ut_utils import ForgeTestCase from forge import UnexpectedSetattr -from forge.python3_compat import xrange class WildcardTest(ForgeTestCase): def tearDown(self): @@ -44,7 +43,7 @@ class WildcardTest(ForgeTestCase): wc = self.forge.create_wildcard_mock() wc.a = 2 self.forge.replay() - self.assertEquals(wc.a, 2) + self.assertEqual(wc.a, 2) with self.assertRaises(UnexpectedSetattr): wc.a = 2 with self.assertRaises(UnexpectedSetattr): @@ -56,7 +55,7 @@ class WildcardTest(ForgeTestCase): wc.__forge__.expect_setattr("a", 2) self.forge.replay() wc.a = 2 - self.assertEquals(wc.a, 2) + self.assertEqual(wc.a, 2) def test__repr(self): name = 'some_name' wc = self.forge.create_wildcard_mock(name) @@ -73,9 +72,9 @@ class WildcardTest(ForgeTestCase): with wc: f() wc.__len__().and_return(666) - wc.__iter__().and_return(iter(xrange(10))) + wc.__iter__().and_return(iter(range(10))) self.forge.replay() with wc: f() - self.assertEquals(len(wc), 666) - self.assertEquals([x for x in wc], list(range(10))) + self.assertEqual(len(wc), 666) + self.assertEqual([x for x in wc], list(range(10))) diff --git a/tests/ut_utils/__init__.py b/tests/ut_utils/__init__.py index 2492c29a5a9c5b052559fd2cbcead3a42dd4a325..54bbb714c1ef746e90eb6113a4d29856b5b09aa8 100644 --- a/tests/ut_utils/__init__.py +++ b/tests/ut_utils/__init__.py @@ -1,23 +1,15 @@ from functools import wraps import sys -import types -import platform from forge import Forge -from forge.python3_compat import IS_PY3 -try: - import unittest2 as unittest -except ImportError: - import unittest + +import unittest class TestCase(unittest.TestCase): pass -if IS_PY3: - from io import BytesIO as BinaryObjectClass - assert not hasattr(sys.modules[BinaryObjectClass.__module__], "__file__") -else: - from cStringIO import StringIO as BinaryObjectClass +from io import BytesIO as BinaryObjectClass +assert not hasattr(sys.modules[BinaryObjectClass.__module__], "__file__") class ForgeTestCase(TestCase): def setUp(self): @@ -31,7 +23,7 @@ class ForgeTestCase(TestCase): super(ForgeTestCase, self).tearDown() def assertNoMoreCalls(self): expected = self.forge.queue.get_expected() - self.assertEquals(len(expected), 0, "len=%d != 0, expected_events=%s, queue=%s" % (len(expected), + self.assertEqual(len(expected), 0, "len=%d != 0, expected_events=%s, queue=%s" % (len(expected), repr(expected), repr(self.forge.queue))) class Method(object): @@ -59,9 +51,7 @@ class StaticMethod(Method): def build_new_style_class(methods=()): return type('NewStyleClass', (object,), _get_class_dict(methods)) def build_old_style_class(methods=()): - if IS_PY3: - return build_new_style_class(methods) - return types.ClassType('OldStyleClass', (), _get_class_dict(methods)) + return build_new_style_class(methods) def _get_class_dict(methods): return dict((method.name, method.get_function()) for method in methods) diff --git a/tox.ini b/tox.ini index 91cf80426efcfb06cde9e7af575585fb4808d745..ed51a4c4c7ff32003c15c4ccad56fea450322487 100644 --- a/tox.ini +++ b/tox.ini @@ -1,10 +1,6 @@ [tox] -envlist = py27,py33,py34,py35 +envlist = py36,py37,py38,py39,py310 [testenv] -deps=nose -commands=nosetests - -[testenv:py26] -deps=unittest2 - nose +deps=pytest +commands=pytest