Why python mock patch doesn’t work?

Access bar using spike.bar. Imported bar is not affected by mock.patch. from unittest import TestCase import unittest from mock import patch, MagicMock from spike import T1 import spike # <—- class TestShit(TestCase): @patch(‘spike.T1.foo’, MagicMock(return_value=”patched”)) def test_foo(self): foo = T1().get_foo() self.assertEqual(‘patched’, foo) @patch(‘spike.bar’) def test_bar(self, mock_obj): mock_obj.return_value=”patched” bar = spike.bar() # <—– self.assertEqual(‘patched’, bar) if __name__ … Read more

Getting Python’s unittest results in a tearDown() method

As of March 2022 this answer is updated to support Python versions between 3.4 and 3.11 (including the newest development Python version). Classification of errors / failures is the same that is used in the output unittest. It works without any modification of code before tearDown(). It correctly recognizes decorators skipIf() and expectedFailure. It is … Read more

How to change the message in a Python AssertionError?

assert expression, info For instance, >>> assert False, “Oopsie” Traceback (most recent call last): File “<stdin>”, line 1, in <module> AssertionError: Oopsie From the docs: Assert statements are a convenient way to insert debugging assertions into a program: assert_stmt ::= “assert” expression [“,” expression] The simple form, assert expression, is equivalent to if __debug__: if … Read more

Scrapy Unit Testing

The way I’ve done it is create fake responses, this way you can test the parse function offline. But you get the real situation by using real HTML. A problem with this approach is that your local HTML file may not reflect the latest state online. So if the HTML changes online you may have … Read more

nose vs pytest – what are the (subjective) differences that should make me pick either? [closed]

I used to use Nose because it was the default with Pylons. I didn’t like it at all. It had configuration tendrils in multiple places, virtually everything seemed to be done with an underdocumented plugin which made it all even more indirect and confusing, and because it did unittest tests by default, it regularly broke … Read more

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