Flashcards
python test doubles mocks fakes
Cards in this deck (14)
- 1what is a test double>?a stand in for an actual class
- 2what are the different types of test doubles?-dummy object
-stubs
-spys
-mocks
-fakes - 3what is a test stub?any object that has no logic or behavior, ex) just returns values
and
it has same interface than class mocked - 4how do you start using stubs?from unittest.mock import Mock
variable = Mock(ClassToMock) - 5how do you represent a file in memory?import io
x=io.StringIO(self.text) - 6what is a fake?a test double that uses both logic and functionality, which of course we are using for testing purposes
- 7what are some common uses for fakes?replace
file
db
webserver - 8what is a mock?mocks are like stubs, but they make assertions what happens in test cases. ie they can cause the test to fail.
- 9what are the 3 types of assertions?check return value or exception
check a state change (use public api)
check a method call (use mock or spy) - 10what can a mock do with function calls?test if the method was called with a particular object
and
particular arguments - 11how do you test if a function was called using a mock?set a boolean if a function is called, and assert that boolean value is true
- 12what is a spy?same as a mock, but watches and records test status, it can let test run, a mock will fail straight away. you can check spy status later
- 13how do you use monkeypatching to change classes at runtime?with patch('module.Classname') as test_class:
test_instance=Mock()
test_instance.myvar=1
or use decorator @patch('module.Classname') - 14when would you use monkeypatching over refactoring class to make it use mocks?when you cannot change class signatures
Information
- Description
- python (python unit testing) Flashcards on python test doubles mocks fakes, created by Andrew Sperazza on 18/01/2017.
- Created by
- Andrew Sperazza
- Views
- 0
- Rating