O'Reilly logo

Python Testing Beginner's Guide by Daniel Arbuckle

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - passing a mock object as self

  1. Remember the testable class that we used, among other things, to demonstrate how it can be difficult to separate methods so we can deal with them as units? Although we saw this before in Chapter 3, here it is again:
    class testable:
    def method1(self, number):
    number += 4
    number **= 0.5
    number *= 7
    return number
    def method2(self, number):
    return ((number * 2) ** 1.27) * 0.3
    def method3(self, number):
    return self.method1(number) + self.method2(number)
    def method4(self):
    return self.method3(id(self))
    
  2. We're going to write a unit test for method3. Like all unit tests, it needs to not involve any code from any other unit, which in this case means that self.method1 and self.method2 need to be mock objects. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required