Mocking two functions with patch for a unit test

You can simplify your test by using the patch decorator and nesting them like so (they are MagicMock objects by default): from unittest.mock import patch @patch(‘cPickle.dump’) @patch(‘__builtin__.open’) def test_write_out(mock_open, mock_pickle): path=”~/collection” f = mock_open.return_value f.method.return_value = path write_out(path, ‘data’) mock_open.assert_called_once_with(‘~/collection’, ‘wb’) mock_pickle.assert_called_once_with(‘data’, f) f.close.assert_any_call() Calls to a MagicMock instance return a new MagicMock instance, so … Read more

How should I verify a log message when testing Python code under nose?

From python 3.4 on, the standard unittest library offers a new test assertion context manager, assertLogs. From the docs: with self.assertLogs(‘foo’, level=”INFO”) as cm: logging.getLogger(‘foo’).info(‘first message’) logging.getLogger(‘foo.bar’).error(‘second message’) self.assertEqual(cm.output, [‘INFO:foo:first message’, ‘ERROR:foo.bar:second message’])

Python Nose Import Error

You’ve got an __init__.py in your top level directory. That makes it a package. If you remove it, your nosetests should work. If you don’t remove it, you’ll have to change your import to import dir.foo, where dir is the name of your directory.

How do I run a single test with Nose in Pylons

nosetests appname.tests.functional.test_controller should work, where the file is named test_controller.py. To run a specific test class and method use a path of the form module.path:ClassNameInFile.method_name, that is, with a colon separating the module/file path and the objects within the file. module.path is the relative path to the file (e.g. tests/my_tests.py:ClassNameInFile.method_name).

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)