I'm trying to learn how to use Mocks for Python. However I've been struggling with some basic application of it.
Let's say our piece of code that I want to test is this:
class ProductionClass:
def method(self):
newone=ProductionClass()
newone.something(1, 2, 3)
def something(self, a, b, c):
pass
def __init__(self):
print("Test")
Which have a method which is simply create a new object of itself and calls a method of that class.
import unittest
import unittest.mock
from ProductionClass import *
from unittest.mock import *
class TestProduction(unittest.TestCase):
def test_one(self):
real = ProductionClass()
real.something = MagicMock()
real.method()
real.something.assert_called_once_with(1, 2, 3)
if __name__ == '__main__':
unittest.main()
Once again, this is a very simple UnitTest, basically copypasted from 26.5.1.1 of http://ift.tt/17xw7Po .
However, this would test if real.something has been called, meanwhile the one i really want to test is if newone.something has been called.
Considering newone is created later when we actually call method()-method how do I use mock to test it?
Aucun commentaire:
Enregistrer un commentaire