I am trying to mock what supplies the default value for the luigi parameter.
A dumb example showing what I'm trying to do:
Task:
import luigi
from bar import Bar
bar = Bar()
class Baz(luigi.Task):
qux = luigi.Parameter(default=bar.bar())
def baz(self):
return self.qux;
def foo(self):
return bar.bar()
Unit Test Code:
import unittest
from mock import Mock, patch
from sut.baz import Baz
class TestMocking(unittest.TestCase):
def test_baz_bar(self):
self.assertEquals("bar", Baz().baz())
@patch('sut.baz.bar')
def test_patched_baz(self, mock_bar):
mock_bar.bar = Mock(return_value="foo")
self.assertEquals("foo", (Baz().baz()))
@patch('sut.baz.bar')
def test_patched_foo(self, mock_bar):
mock_bar.bar = Mock(return_value="foo")
self.assertEquals("foo", (Baz().foo()))
It seems that the logic of luigi.Parameter happens earlier than the patch.
This example test_patched_foopasses, but test_patched_bazfails. Thus, the patch occurs, but occurs after a call from a string luigi.Parameter(default=bar.bar()).
Is it possible to mock and correct something called in this way?
source
share