How do you mock patch a python class and get a new Mock object for each instantiation?

Here’s a quick’n’dirty example to get you going: import mock import unittest class ClassToPatch(): def __init__(self, *args): pass def some_func(self): return id(self) class UUT(): def __init__(self, *args): resource_1 = ClassToPatch() resource_2 = ClassToPatch() self.test_property = (resource_1.some_func(), resource_2.some_func()) class TestCase1(unittest.TestCase): @mock.patch(‘__main__.ClassToPatch’, autospec = True) def test_1(self, mock1): ctpMocks = [mock.Mock(), mock.Mock()] ctpMocks[0].some_func.return_value = “funky” ctpMocks[1].some_func.return_value = … Read more

What is the correct order for actual and expected in pytest?

JUnit assertEquals(expected, actual) Pytest (Pycharm) Comments have implied this may be more an issue with the way PyCharm displays the message than pytest itself – ie. this message may not exist outside of PyCharm… assert actual == expected For example: def test_actual_expected(): expected = 4 actual = 2+1 assert actual == expected Will fail with … Read more

Suppress print output in unittests

Call your unittest with option “-b” – buffer stdout and stderr Foo.py class Foo: def bar(self): print “bar” return 7 test.py import unittest from Foo import Foo class test_Foo(unittest.TestCase): def test_bar(self): obj = Foo() res = obj.bar() self.assertEqual(res, 7) if __name__ == “__main__”: unittest.main() Run it with -b option $ python test.py -b . ———————————————————————- … Read more

How do I test if a certain log message is logged in a Django test case?

Using the mock module for mocking the logging module or the logger object. When you’ve done that, check the arguments with which the logging function is called. For example, if you code looks like this: import logging logger = logging.getLogger(‘my_logger’) logger.error(“Your log message here”) it would look like: from unittest.mock import patch # For python … Read more

Trying to implement python TestSuite

you want to use a testsuit. So you need not call unittest.main(). Use of testsuit should be like this: #import usertest #import configtest # first test import unittest # second test class ConfigTestCase(unittest.TestCase): def setUp(self): print ‘stp’ ##set up code def runTest(self): #runs test print ‘stp’ def suite(): “”” Gather all the tests from this … Read more

numpy testing assert array NOT equal

If you want to use specifically NumPy testing, then you can use numpy.testing.assert_array_equal together with numpy.testing.assert_raises for the opposite result. For example: assert_raises(AssertionError, assert_array_equal, array_1, array_2) Also there is numpy.testing.utils.assert_array_compare (it is used by numpy.testing.assert_array_equal), but I don’t see it documented anywhere, so use with caution. This one will check that every element is different, … Read more

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