This is literally the whole unittest.<br><br><br> import unittest<br> from mock import patch<br> from yaml import safe_load<br> <br> from graphyte.tests.do_config import read_config<br> from graphyte.graphapp.model.codebundle import CodeBundle<br>
<br> test_config = read_config()<br> This is literally the whole unittest.<br><br><br> import unittest<br> from mock import patch<br> from yaml import safe_load<br> <br> from graphyte.tests.do_config import read_config<br>
from graphyte.graphapp.model.codebundle import CodeBundle<br> <br> test_config = read_config()<br> <br> class TestCodebundleBBDSModel(unittest.TestCase):<br> def setUp(self):<br> self.patcher1 = patch('graphyte.graphapp.model.codebundle.CodeBundle', autospec=True)<br>
self.cb = self.patcher1.start()<br> <br> def tearDown(self):<br> self.patcher1.stop()<br> <br> def test_bbds_list(self):<br> self.cb.return_value.tempdir = test_config['unit-fixtures']<br>
bbds_list = self.cb.return_value.bbds_list.return_value<br> expected = ['123', '456']<br> <br> self.assertEqual(expected, bbds_list)<br><br>instead I get <br><br> File "test_codebundles.py", line 26, in test_bbds_list<br>
self.assertEqual(expected, bbds_list)<br> AssertionError: ['123', '456'] != <MagicMock name='CodeBundle().bbds_list()' id='173575436'><br><br><br>I tried with and without `return_value` and with and without `()`, but it yields nothing useful.<br>
<br>Here is the system under test<br><br> def bbds_list(self):<br> assert self.setup_tempdir()<br> datalist_path = os.path.join(self.tempdir, '.datalist.yaml')<br> with open(datalist_path, 'r') as f:<br>
configs = safe_load(f)<br> bbids = configs['bbids']<br> return list(bbids.iterkeys())<br><br>Another problem I encouter is I cannot make the attribute `self.tempdir` on the fly.<br>
<br>Any idea what's wrong with my test code?<br><br> class TestCodebundleBBDSModel(unittest.TestCase):<br> def setUp(self):<br> self.patcher1 = patch('graphyte.graphapp.model.codebundle.CodeBundle', autospec=True)<br>
self.cb = self.patcher1.start()<br> <br> def tearDown(self):<br> self.patcher1.stop()<br> <br> def test_bbds_list(self):<br> self.cb.return_value.tempdir = test_config['unit-fixtures']<br>
bbds_list = self.cb.return_value.bbds_list.return_value<br> expected = ['123', '456']<br> <br> self.assertEqual(expected, bbds_list)<br><br>instead I get <br><br> File "test_codebundles.py", line 26, in test_bbds_list<br>
self.assertEqual(expected, bbds_list)<br> AssertionError: ['123', '456'] != <MagicMock name='CodeBundle().bbds_list()' id='173575436'><br><br><br>I tried with and without `return_value` and with and without `()`, but it yields nothing useful.<br>
<br>Here is the system under test<br><br> def bbds_list(self):<br> assert self.setup_tempdir()<br> datalist_path = os.path.join(self.tempdir, '.datalist.yaml')<br> with open(datalist_path, 'r') as f:<br>
configs = safe_load(f)<br> bbids = configs['bbids']<br> return list(bbids.iterkeys())<br><br>Another problem I encounter is I cannot make the attribute `self.tempdir` on the fly. I want to asself self.cb the object an attribute called `self.tempdir`.<br>
<br>Any idea what's wrong with my test code? I am confused why we always need to use `return_value`. <br><br>