import unittest
import pmock
import testsupport
class VerificationErrorTest(unittest.TestCase):
def test_create_error(self):
class Mocker:
def __str__(self): return "matchers"
error = pmock.VerificationError.create_error("problem", Mocker())
self.assertEqual(error.msg, "problem: matchers")
class MatchErrorTest(unittest.TestCase):
class MockInvocation:
def __str__(self): return "call"
class Mock:
def __init__(self, invokables_str): self._str = invokables_str
def invokables_str(self): return self._str
def test_empty_invokables(self):
error = pmock.MatchError.create_error("msg",
self.MockInvocation(),
self.Mock(""))
self.assertEqual(error.msg, "msg\ninvoked call")
def test_non_empty_invokables(self):
error = pmock.MatchError.create_error("msg",
self.MockInvocation(),
self.Mock("invokables"))
self.assertEqual(error.msg, "msg\ninvoked call\nin:\ninvokables")
class ArgumentsMatcherTestMixin(object):
def __init__(self, matcher_class):
self.matcher_class = matcher_class
def test_matches_arguments(self):
arg1 = []
args_matcher = self.matcher_class((pmock.same(arg1),), {})
self.assert_(
args_matcher.matches(pmock.Invocation("snake", (arg1,), {})))
def test_matches_keyword_arguments(self):
arg2 = []
args_matcher = self.matcher_class((), {"food": pmock.same(arg2)})
invocation = pmock.Invocation("snake", (), {"food": arg2})
self.assert_(args_matcher.matches(invocation))
def test_matches_both_types_of_arguments(self):
arg1 = []
arg2 = []
args_matcher = self.matcher_class((pmock.same(arg1),),
{"food": pmock.same(arg2)})
invocation = pmock.Invocation("snake", (arg1,), {"food": arg2})
self.assert_(args_matcher.matches(invocation))
def test_insufficient_arguments(self):
args_matcher = self.matcher_class((pmock.eq("slither"),), {})
self.assert_(
not args_matcher.matches(pmock.Invocation("snake", (), {})))
def test_insufficient_keyword_arguments(self):
args_matcher = self.matcher_class((), {"food": pmock.eq("goat")})
self.assert_(
not args_matcher.matches(pmock.Invocation("snake", (), {})))
def test_unmatched_argument(self):
class UnmatchedConstraint:
def eval(self, invocation): return False
args_matcher = self.matcher_class((UnmatchedConstraint(),), {})
invocation = pmock.Invocation("snake", ("slither",), {})
self.assert_(not args_matcher.matches(invocation))
def test_unmatched_keyword_argument(self):
class UnmatchedConstraint:
def eval(self, invocation): return False
args_matcher = self.matcher_class((), {"food": UnmatchedConstraint()})
invocation = pmock.Invocation("snake", (), {"food": "goat"})
self.assert_(not args_matcher.matches(invocation))
class AllArgumentsMatcherTest(ArgumentsMatcherTestMixin, unittest.TestCase):
def __init__(self, *args):
ArgumentsMatcherTestMixin.__init__(self, pmock.AllArgumentsMatcher)
unittest.TestCase.__init__(self, *args)
def test_extra_arguments(self):
args_matcher = pmock.AllArgumentsMatcher((pmock.eq("slither"),), {})
self.assert_(not args_matcher.matches(
pmock.Invocation("snake", ("slither", "hiss"), {})))
def test_extra_keyword_arguments(self):
args_matcher = pmock.AllArgumentsMatcher((),
{"food": pmock.eq("goat")})
self.assert_(not args_matcher.matches(
pmock.Invocation("snake", (), {"food": "goat", "colour": "red"})))
def test_no_arguments(self):
args_matcher = pmock.AllArgumentsMatcher()
self.assert_(args_matcher.matches(pmock.Invocation("snake", (), {})))
self.assert_(
not args_matcher.matches(pmock.Invocation("snake", ("hiss",), {})))
self.assert_(not args_matcher.matches(
pmock.Invocation("snake", (), {"food": "goat"})))
def test_str(self):
matcher = pmock.AllArgumentsMatcher((pmock.eq("slither"),),
{"food": pmock.eq("goat")})
self.assertEqual(str(matcher),
"(pmock.eq('slither'), food=pmock.eq('goat'))")
def test_empty_str(self):
self.assertEqual(str(pmock.AllArgumentsMatcher()), "()")
class LeastArgumentsMatcherTest(ArgumentsMatcherTestMixin, unittest.TestCase):
def __init__(self, *args):
ArgumentsMatcherTestMixin.__init__(self, pmock.LeastArgumentsMatcher)
unittest.TestCase.__init__(self, *args)
def test_extra_arguments(self):
args_matcher = pmock.LeastArgumentsMatcher((pmock.eq("slither"),), {})
self.assert_(args_matcher.matches(
pmock.Invocation("snake", ("slither", "hiss"), {})))
def test_extra_keyword_arguments(self):
args_matcher = pmock.LeastArgumentsMatcher((),
{"food": pmock.eq("goat")})
self.assert_(args_matcher.matches(
pmock.Invocation("snake", (), {"food": "goat", "colour": "red"})))
def test_any_arguments(self):
args_matcher = pmock.LeastArgumentsMatcher()
self.assert_(args_matcher.matches(pmock.Invocation("snake", (), {})))
self.assert_(
args_matcher.matches(pmock.Invocation("snake", ("hiss",), {})))
self.assert_(args_matcher.matches(
pmock.Invocation("snake", ("constrict",), {"food": "goat"})))
def test_str(self):
matcher = pmock.LeastArgumentsMatcher((pmock.eq("slither"),),
{"food": pmock.eq("goat")})
self.assertEqual(str(matcher),
"(pmock.eq('slither'), food=pmock.eq('goat'), ...)")
def test_empty_str(self):
self.assertEqual(str(pmock.LeastArgumentsMatcher()), "(...)")
class InvocationMockerTest(unittest.TestCase):
class MockMatcher:
def __init__(self, matches):
self._matches = matches
def matches(self, invocation):
self.matches_invocation = invocation
return self._matches
def invoked(self, invocation):
self.invoked_invocation = invocation
def test_matches(self):
mocker = pmock.InvocationMocker(self.MockMatcher(True))
self.assert_(mocker.matches(pmock.Invocation("duck", (), {})))
def test_unmatched(self):
mocker = pmock.InvocationMocker(self.MockMatcher(False))
self.assert_(not mocker.matches(pmock.Invocation("duck", (), {})))
def test_added_matcher_unmatched(self):
mocker = pmock.InvocationMocker(self.MockMatcher(True))
mocker.add_matcher(self.MockMatcher(False))
self.assert_(not mocker.matches(pmock.Invocation("duck", (), {})))
def test_matches_passes_invocation_to_matcher(self):
matcher = self.MockMatcher(True)
mocker = pmock.InvocationMocker(matcher)
invocation = pmock.Invocation("duck", (), {})
mocker.matches(invocation)
self.assertEqual(matcher.matches_invocation, invocation)
def test_no_stub_returns_none(self):
mocker = pmock.InvocationMocker(self.MockMatcher(True))
self.assert_(mocker.invoke(pmock.Invocation("duck", (), {})) is None)
def test_invoke_returns_stubs_value(self):
class MockStub:
def invoke(self, invocation): return 'value'
mocker = pmock.InvocationMocker(self.MockMatcher(True))
mocker.set_stub(MockStub())
self.assert_(mocker.invoke(pmock.Invocation("duck", (), {}))
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
共74个文件
html:63个
py:7个
css:3个
Python单元测试库,A mock object is created to represent a real object for the purposes of a unit test. Using a mock object rather than the real object can be useful if the real object is difficult to construct, hasn't been written yet or causes awkward side effects in use. import pmock ... mock = pmock.Mock()
资源推荐
资源详情
资源评论
收起资源包目录
pmock-0.3.tar.gz (74个子文件)
pmock-0.3
examples
errors.py 497B
greeting.py 2KB
TODO 469B
src
pmock.py 21KB
unit_tests.py 35KB
acceptance_tests.py 16KB
testsupport.py 274B
setup.py 996B
doc
overview.html 6KB
api
public
trees.html 5KB
pmock-module.html 13KB
exceptions.Exception-class.html 5KB
exceptions.AssertionError-class.html 5KB
frames.html 353B
help.html 10KB
toc-everything.html 2KB
exceptions.StandardError-class.html 5KB
__builtin__.type-class.html 24KB
epydoc.css 4KB
__builtin__.object-class.html 11KB
unittest.TestCase-class.html 35KB
toc-pmock-module.html 2KB
pmock.MockTestCase-class.html 22KB
pmock.Mock-class.html 14KB
indices.html 21KB
toc.html 979B
index.html 353B
private
trees.html 8KB
pmock.StubInvocationMatcher-class.html 8KB
pmock-module.html 25KB
exceptions.Exception-class.html 5KB
pmock.SameConstraint-class.html 8KB
pmock.AbstractArgumentsMatcher-class.html 9KB
exceptions.AssertionError-class.html 5KB
frames.html 353B
pmock.EqConstraint-class.html 8KB
help.html 10KB
toc-everything.html 5KB
pmock.AllArgumentsMatcher-class.html 10KB
pmock.InvocationMocker-class.html 9KB
pmock.MethodMatcher-class.html 8KB
pmock.StringContainsConstraint-class.html 8KB
pmock.FunctorConstraint-class.html 8KB
pmock.OnceInvocationMatcher-class.html 9KB
exceptions.StandardError-class.html 5KB
pmock.DefaultStub-class.html 8KB
__builtin__.type-class.html 24KB
epydoc.css 4KB
__builtin__.object-class.html 13KB
pmock.LeastArgumentsMatcher-class.html 10KB
pmock.BoundMethod-class.html 8KB
unittest.TestCase-class.html 36KB
toc-pmock-module.html 5KB
pmock.MockTestCase-class.html 22KB
pmock.AtLeastOnceInvocationMatcher-class.html 9KB
pmock.Mock-class.html 14KB
pmock.DefinitionError-class.html 6KB
pmock.Proxy-class.html 8KB
pmock.ReturnValueStub-class.html 8KB
pmock.InvokedRecorderMatcher-class.html 9KB
indices.html 49KB
pmock.MatchError-class.html 6KB
toc.html 978B
pmock.Error-class.html 6KB
index.html 353B
pmock.NotCalledInvocationMatcher-class.html 8KB
pmock.InvocationMockerBuilder-class.html 15KB
pmock.Invocation-class.html 7KB
pmock.RaiseExceptionStub-class.html 8KB
pmock.VerificationError-class.html 6KB
pmock.InvokedAfterMatcher-class.html 8KB
epydoc.css 4KB
index.html 374B
index.html 2KB
共 74 条
- 1
资源评论
- zzk19952016-06-02很不错。可用。
- abu4cn2014-10-05很不错。可用。
chinablueker
- 粉丝: 1
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功