Skip to content

Commit

Permalink
Reorganize unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ollipa committed Dec 14, 2021
1 parent c5c736e commit 60c411d
Show file tree
Hide file tree
Showing 23 changed files with 3,175 additions and 3,137 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Generate report
run: |
poetry run coverage run --parallel-mode --source flexmock -m unittest tests/test_flexmock.py
export PYTHONPATH=$PWD
poetry run coverage run --parallel-mode --source flexmock tests/test_unittest.py
poetry run coverage run --parallel-mode --source flexmock tests/test_doctest.py
poetry run coverage run --parallel-mode --source flexmock -m pytest tests/test_pytest.py
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ unittest:
@printf '\n\n*****************\n'
@printf '$(color)Running unittest$(off)\n'
@printf '*****************\n'
$(PYTHON) -m unittest tests/test_flexmock.py
$(PYTHON) tests/test_unittest.py

.PHONY: doctest
doctest:
Expand All @@ -51,7 +51,7 @@ twisted:
@printf '\n\n*****************\n'
@printf '$(color)Running twisted tests$(off)\n'
@printf '*****************\n'
$(PYTHON) -c "from twisted.scripts.trial import run; run();" tests/test_flexmock.py
$(PYTHON) -c "from twisted.scripts.trial import run; run();" tests/test_integrations.py

.PHONY: mypy
mypy:
Expand Down
33 changes: 33 additions & 0 deletions tests/features/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Tests for flexmock.
Tests under this module should not depend on any test runner.
"""
from .arg_matching import ArgumentMatchingTestCase
from .call_count import CallCountTestCase
from .common import CommonTestCase
from .conditional import ConditionalAssertionsTestCase
from .derived import DerivedTestCase
from .mocking import MockingTestCase
from .ordered import OrderedTestCase
from .proxied import ProxiedTestCase
from .spying import SpyingTestCase
from .stubbing import StubbingTestCase
from .teardown import TeardownTestCase


class FlexmockTestCase( # pylint: disable=too-many-ancestors
ArgumentMatchingTestCase,
CallCountTestCase,
CommonTestCase,
ConditionalAssertionsTestCase,
DerivedTestCase,
MockingTestCase,
OrderedTestCase,
ProxiedTestCase,
SpyingTestCase,
StubbingTestCase,
TeardownTestCase,
):
"""This class should be imported by other test modules to run full flexmock
test suite.
"""
255 changes: 255 additions & 0 deletions tests/features/arg_matching.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
"""Tests for argument matching."""
# pylint: disable=missing-docstring,no-self-use,no-member
import re

from flexmock import exceptions, flexmock
from flexmock._api import flexmock_teardown
from tests import some_module
from tests.some_module import SomeClass
from tests.utils import assert_raises


class ArgumentMatchingTestCase:
def test_arg_matching_works_with_regexp(self):
class Foo:
def method(self, arg1, arg2):
pass

instance = Foo()
flexmock(instance).should_receive("method").with_args(
re.compile("^arg1.*asdf$"), arg2=re.compile("f")
).and_return("mocked")
assert instance.method("arg1somejunkasdf", arg2="aadsfdas") == "mocked"

def test_arg_matching_with_regexp_fails_when_regexp_doesnt_match_karg(self):
class Foo:
def method(self, arg1, arg2):
pass

instance = Foo()
flexmock(instance).should_receive("method").with_args(
re.compile("^arg1.*asdf$"), arg2=re.compile("a")
).and_return("mocked")
with assert_raises(
exceptions.MethodSignatureError,
(
"Arguments for call method did not match expectations:\n"
' Received call:\tmethod("arg1somejunkasdfa", arg2="a")\n'
" Expected call[1]:\tmethod(arg2=/a/, arg1=/^arg1.*asdf$/)"
),
):
instance.method("arg1somejunkasdfa", arg2="a")

def test_arg_matching_with_regexp_fails_when_regexp_doesnt_match_kwarg(self):
class Foo:
def method(self, arg1, arg2):
pass

instance = Foo()
flexmock(instance).should_receive("method").with_args(
re.compile("^arg1.*asdf$"), arg2=re.compile("a")
).and_return("mocked")
with assert_raises(
exceptions.MethodSignatureError,
(
"Arguments for call method did not match expectations:\n"
' Received call:\tmethod("arg1somejunkasdf", arg2="b")\n'
" Expected call[1]:\tmethod(arg2=/a/, arg1=/^arg1.*asdf$/)"
),
):
instance.method("arg1somejunkasdf", arg2="b")

def test_module_level_function_with_kwargs(self):
flexmock(some_module).should_receive("module_function").with_args(1, y="expected")
with assert_raises(
exceptions.FlexmockError,
(
"Arguments for call module_function did not match expectations:\n"
' Received call:\tmodule_function(1, y="not expected")\n'
' Expected call[1]:\tmodule_function(y="expected", x=1)'
),
):
some_module.module_function(1, y="not expected")

def test_flexmock_should_match_types_on_multiple_arguments(self):
class Foo:
def method1(self, arg1, arg2):
pass

instance = Foo()
flexmock(instance).should_receive("method1").with_args(str, int).and_return("ok")
assert instance.method1("some string", 12) == "ok"
with assert_raises(
exceptions.MethodSignatureError,
(
"Arguments for call method1 did not match expectations:\n"
" Received call:\tmethod1(12, 32)\n"
" Expected call[1]:\tmethod1(arg1=<class 'str'>, arg2=<class 'int'>)"
),
):
instance.method1(12, 32)
flexmock(instance).should_receive("method1").with_args(str, int).and_return("ok")
with assert_raises(
exceptions.MethodSignatureError,
(
"Arguments for call method1 did not match expectations:\n"
' Received call:\tmethod1(12, "some string")\n'
" Expected call[1]:\tmethod1(arg1=<class 'str'>, arg2=<class 'int'>)\n"
" Expected call[2]:\tmethod1(arg1=<class 'str'>, arg2=<class 'int'>)"
),
):
instance.method1(12, "some string")
flexmock(instance).should_receive("method1").with_args(str, int).and_return("ok")
with assert_raises(
exceptions.MethodSignatureError,
(
"Arguments for call method1 did not match expectations:\n"
' Received call:\tmethod1("string", 12, 14)\n'
" Expected call[1]:\tmethod1(arg1=<class 'str'>, arg2=<class 'int'>)\n"
" Expected call[2]:\tmethod1(arg1=<class 'str'>, arg2=<class 'int'>)\n"
" Expected call[3]:\tmethod1(arg1=<class 'str'>, arg2=<class 'int'>)"
),
):
instance.method1("string", 12, 14)

def test_flexmock_should_match_types_on_multiple_arguments_generic(self):
class Foo:
def method1(self, a, b, c): # pylint: disable=invalid-name
pass

instance = Foo()
flexmock(instance).should_receive("method1").with_args(object, object, object).and_return(
"ok"
)
assert instance.method1("some string", None, 12) == "ok"
assert instance.method1((1,), None, 12) == "ok"
assert instance.method1(12, 14, []) == "ok"
assert instance.method1("some string", "another one", False) == "ok"
with assert_raises(
exceptions.MethodSignatureError,
(
"Arguments for call method1 did not match expectations:\n"
' Received call:\tmethod1("string", 12)\n'
" Expected call[1]:\tmethod1(a=<class 'object'>, "
"b=<class 'object'>, c=<class 'object'>)"
),
):
instance.method1("string", 12) # pylint: disable=no-value-for-parameter
flexmock_teardown()
flexmock(instance).should_receive("method1").with_args(object, object, object).and_return(
"ok"
)
with assert_raises(
exceptions.MethodSignatureError,
(
"Arguments for call method1 did not match expectations:\n"
' Received call:\tmethod1("string", 12, 13, 14)\n'
" Expected call[1]:\tmethod1(a=<class 'object'>, "
"b=<class 'object'>, c=<class 'object'>)"
),
):
instance.method1("string", 12, 13, 14)

def test_flexmock_should_match_types_on_multiple_arguments_classes(self):
class Foo:
def method1(self, a, b): # pylint: disable=invalid-name
pass

class Bar:
pass

foo_instance = Foo()
bar_instance = Bar()
flexmock(foo_instance).should_receive("method1").with_args(object, Bar).and_return("ok")
assert foo_instance.method1("some string", bar_instance) == "ok"
with assert_raises(
exceptions.MethodSignatureError,
re.compile(
"Arguments for call method1 did not match expectations:\n"
r' Received call:\tmethod1\(.+\.<locals>\.Bar object at 0x.+>, "some string"\)\n'
r" Expected call\[1\]:\tmethod1\(a=<class 'object'>, b=<class.+\.<locals>\.Bar'>\)"
),
):
foo_instance.method1(bar_instance, "some string")
flexmock_teardown()
flexmock(foo_instance).should_receive("method1").with_args(object, Bar).and_return("ok")
with assert_raises(
exceptions.MethodSignatureError,
re.compile(
"Arguments for call method1 did not match expectations:\n"
r' Received call:\tmethod1\(12, "some string"\)\n'
r" Expected call\[1\]:\tmethod1\(a=<class 'object'>, b=<class.+\.<locals>\.Bar'>\)"
),
):
foo_instance.method1(12, "some string")

def test_flexmock_should_match_keyword_arguments(self):
class Foo:
def method1(self, arg1, **kwargs):
pass

instance = Foo()
flexmock(instance).should_receive("method1").with_args(1, arg3=3, arg2=2).twice()
instance.method1(1, arg2=2, arg3=3)
instance.method1(1, arg3=3, arg2=2)
flexmock_teardown()
flexmock(instance).should_receive("method1").with_args(1, arg3=3, arg2=2)
with assert_raises(
exceptions.MethodSignatureError,
(
"Arguments for call method1 did not match expectations:\n"
" Received call:\tmethod1(arg2=2, arg3=3)\n"
" Expected call[1]:\tmethod1(arg3=3, arg2=2, arg1=1)"
),
):
instance.method1(arg2=2, arg3=3) # pylint: disable=no-value-for-parameter
flexmock_teardown()
flexmock(instance).should_receive("method1").with_args(1, arg3=3, arg2=2)
with assert_raises(
exceptions.MethodSignatureError,
(
"Arguments for call method1 did not match expectations:\n"
" Received call:\tmethod1(1, arg2=2, arg3=4)\n"
" Expected call[1]:\tmethod1(arg3=3, arg2=2, arg1=1)"
),
):
instance.method1(1, arg2=2, arg3=4)
flexmock_teardown()
flexmock(instance).should_receive("method1").with_args(1, arg3=3, arg2=2)
with assert_raises(
exceptions.MethodSignatureError,
(
"Arguments for call method1 did not match expectations:\n"
" Received call:\tmethod1(1)\n"
" Expected call[1]:\tmethod1(arg3=3, arg2=2, arg1=1)"
),
):
instance.method1(1)

def test_flexmock_should_call_should_match_keyword_arguments(self):
class Foo:
def method1(self, arg1, arg2=None, arg3=None):
return f"{arg1}{arg2}{arg3}"

instance = Foo()
flexmock(instance).should_call("method1").with_args(1, arg3=3, arg2=2).once()
assert instance.method1(1, arg2=2, arg3=3) == "123"

def test_with_args_with_instance_method(self):
flexmock(SomeClass).should_receive("instance_method_with_args").with_args("red").once()
flexmock(SomeClass).should_receive("instance_method_with_args").with_args("blue").once()
instance = SomeClass()
instance.instance_method_with_args("red")
instance.instance_method_with_args("blue")

def test_with_args_with_class_method(self):
flexmock(SomeClass).should_receive("class_method_with_args").with_args("red").once()
flexmock(SomeClass).should_receive("class_method_with_args").with_args("blue").once()
SomeClass.class_method_with_args("red")
SomeClass.class_method_with_args("blue")

def test_with_args_with_static_method(self):
flexmock(SomeClass).should_receive("static_method_with_args").with_args("red").once()
flexmock(SomeClass).should_receive("static_method_with_args").with_args("blue").once()
SomeClass.static_method_with_args("red")
SomeClass.static_method_with_args("blue")
75 changes: 75 additions & 0 deletions tests/features/call_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
"""Tests for call count modifiers."""
# pylint: disable=missing-docstring,no-self-use,no-member

from flexmock import exceptions, flexmock
from flexmock._api import flexmock_teardown
from tests.utils import assert_raises


class CallCountTestCase:
def test_support_at_least_and_at_most_together(self):
class Foo:
def method(self):
pass

instance = Foo()
flexmock(instance).should_call("method").at_least().once().at_most().twice()
with assert_raises(
exceptions.MethodCallError,
"method() expected to be called at least 1 time and at most 2 times, called 0 times",
):
flexmock_teardown()

flexmock(instance).should_call("method").at_least().once().at_most().twice()
instance.method()
instance.method()
with assert_raises(
exceptions.MethodCallError,
"method() expected to be called at most 2 times, called 3 times",
):
instance.method()

flexmock(instance).should_call("method").at_least().once().at_most().twice()
instance.method()
flexmock_teardown()

flexmock(instance).should_call("method").at_least().once().at_most().twice()
instance.method()
instance.method()
flexmock_teardown()

def test_at_least_cannot_be_used_twice(self):
class Foo:
def method(self):
pass

expectation = flexmock(Foo).should_receive("method")
with assert_raises(exceptions.FlexmockError, "cannot use at_least modifier twice"):
expectation.at_least().at_least()

def test_at_most_cannot_be_used_twice(self):
class Foo:
def method(self):
pass

expectation = flexmock(Foo).should_receive("method")
with assert_raises(exceptions.FlexmockError, "cannot use at_most modifier twice"):
expectation.at_most().at_most()

def test_at_least_cannot_be_specified_until_at_most_is_set(self):
class Foo:
def method(self):
pass

expectation = flexmock(Foo).should_receive("method")
with assert_raises(exceptions.FlexmockError, "cannot use at_most with at_least unset"):
expectation.at_least().at_most()

def test_at_most_cannot_be_specified_until_at_least_is_set(self):
class Foo:
def method(self):
pass

expectation = flexmock(Foo).should_receive("method")
with assert_raises(exceptions.FlexmockError, "cannot use at_least with at_most unset"):
expectation.at_most().at_least()

0 comments on commit 60c411d

Please sign in to comment.