python-doublex

Test doubles framework for Python
Download

python-doublex Ranking & Summary

Advertisement

  • Rating:
  • License:
  • GPL v3
  • Price:
  • FREE
  • Publisher Name:
  • David Villa Alises
  • Publisher web site:
  • https://bitbucket.org/DavidVilla/

python-doublex Tags


python-doublex Description

python-doublex is a powerful test doubles framework for Python.This started as a try to improve and simplify pyDoubles codebase and APISource repository is: https://bitbucket.org/DavidVilla/python-doublexDesign principles- Doubles have not public api specific methods. It avoid silent misspelling.- non-proxified doubles does not require collaborator instances, they may use classes- hamcrest.assert_that used for all assertions- Mock invocation order is required by default- Compatible with old and new style classesDoubles"free" Stub# givenstub = Stub()with stub: stub.foo('hi').returns(10) stub.hello(ANY_ARG).returns(False) stub.bye().raises(SomeException)# whenresult = stub.foo()# thenassert_that(result, 10)"verified" Stubclass Collaborator: def hello(self): return "hello"with Stub(Collaborator) as stub stub.hello().raises(SomeException) stub.foo().returns(True) # raises ApiMismatch exception stub.hello(1).returns(2) # raises ApiMismatch exception"free" Spy# givenwith Spy() as sender: sender.helo().returns("OK")# whensender.send_mail('hi')sender.send_mail('foo@bar.net')# thenassert_that(sender.helo(), "OK")assert_that(sender.send_mail, called())assert_that(sender.send_mail, called().times(2))assert_that(sender.send_mail, called_with('foo@bar.net'))"verified" Spyclass Sender: def say(self): return "hi" def send_mail(self, address, force=True): sender = Spy(Sender)sender.bar() # raises ApiMismatch exceptionsender.send_mail() # raises ApiMismatch exceptionsender.send_mail(wrong=1) # raises ApiMismatch exceptionsender.send_mail('foo', wrong=1) # raises ApiMismatch exceptionProxySpysender = Spy(Sender()) # must give an instancesender.say('boo!') # raises ApiMismatch exceptionassert_that(sender.say(), "hi")assert_that(sender.say, called())"free" Mockwith Mock() as smtp: smtp.helo() smtp.mail(ANY_ARG) smtp.rcpt("bill@apple.com") smtp.data(ANY_ARG).returns(True).times(2)smtp.helo()smtp.mail("poormen@home.net")smtp.rcpt("bill@apple.com")smtp.data("somebody there?")smtp.data("I am afraid..")assert_that(smtp, meets_expectations())"verified" Mockclass SMTP: def helo(self): def mail(self, address): def rcpt(self, address): with Mock(STMP) as smtp: smtp.wrong() # raises ApiMismatch exception smtp.mail() # raises ApiMismatch exceptionstub methodscollaborator = Collaborator()collaborator.foo = method_returning("bye")assertEquals("bye", self.collaborator.foo())collaborator.foo = method_raising(SomeException)collaborator.foo() # raises SomeExceptiondoublex matcherscalledcalled() matches any invocation to a method:spy.Spy()spy.m1()spy.m2(None)spy.m3("hi", 3.0)spy.m4()assert_that(spy.m1, called())assert_that(spy.m2, called())assert_that(spy.m3, called())assert_that(spy.m4, called())called_withcalled_with() matches specific arguments:spy.Spy()spy.m1()spy.m2(None)spy.m3("hi", 3.0)spy.m4()assert_that(spy.m1, called_with())assert_that(spy.m2, called_with(None))assert_that(spy.m3, called_with("hi", 3.0))assert_that(spy.m4, called_with())matchers, matchers, hamcrest matchers...doublex support all hamcrest matchers, and their amazing combinations.checking spied calling argsspy = Spy()spy.foo("abcd")assert_that(spy.foo, called_with(has_length(4)))assert_that(spy.foo, called_with(has_length(greater_than(3))))assert_that(spy.foo, called_with(has_length(less_than(5))))assert_that(spy.foo, is_not(called_with(has_length(greater_than(5)))))stubbingwith Spy() as spy: spy.foo(has_length(less_than(4))).returns('Product's homepage


python-doublex Related Software