python-unittest

How do I patch an object so that all methods are mocked except one?

痴心易碎 提交于 2020-05-11 04:00:10
问题 I have an entry point function call it main on an object that I would like to remain unmocked, since it calls several other methods on the object: class Thing(object): def main(self): self.alpha() self.bravo() def alpha(self): self.charlie() def bravo(self): raise TypeError("Requires Internet connection!") def charlie(self): raise Exception("Bad stuff happens here!") This is pretty straight forward to mock manually: thing = Thing() thing.alpha = MagicMock() thing.bravo = MagicMock() And I can

Recursive unittest discovery with python3 and without __init__.py files

久未见 提交于 2020-05-10 07:15:23
问题 I have project with the following directory structure: . ├── requirements.txt ├── main.py ├── tests ├── unit │ └── test_thing1.py │ └── test_thing2.py └── integration └── test_integration_thing1.py └── test_integration_thing2.py I want to run all tests with one command . If I do python -m unittest discover , no tests are executed. I found this question that suggest adding a __init__.py file to make packages out of the unit and integration folders. The solution works, and all tests are running

Python unittest - Ran 0 tests in 0.000s

拥有回忆 提交于 2020-05-09 19:25:16
问题 So I want to do this code Kata for practice. I want to implement the kata with tdd in separate files: The algorithm: # stringcalculator.py def Add(string): return 1 and the tests: # stringcalculator.spec.py from stringcalculator import Add import unittest class TestStringCalculator(unittest.TestCase): def add_returns_zero_for_emptyString(self): self.assertEqual(Add(' '), 0) if __name__ == '__main__': unittest.main() When running the testfile, I get: Ran 0 tests in 0.000s OK It should return

pytest: setup a mock for every test function

对着背影说爱祢 提交于 2020-04-17 21:32:51
问题 Creating unit tests for a python project we're reaching this kind of 'template' from unittest import TestCase from unittest.mock import patch, Mock @patch('......dependency1') @patch('......dependency2') @patch('......dependencyn') class MyTest(TestCase): def test_1(self, mock1, mock2, mockn): # setup mock1 & mock2... # call the subject case 1 # assert response and/or interactions with mock1 and mock2... def test_2(self, mock1, mock2, mockn): # setup mock1 & mock2... # call the subject case 2

How do I mock the default arguments of a function that is used by the function I'm testing?

a 夏天 提交于 2020-03-23 07:48:11
问题 I have this my_module.py : def _sub_function(do_the_thing=True): if do_the_thing: do_stuff() else: do_something_else() def main_function(): # do some stuff if some_condition: return _sub_function() else: return _sub_function(do_the_thing=False) then I have this test, test_my_module.py : import unittest from unittest import mock import my_module class TestMyModule(unittest.TestCase): @mock.patch.object("my_module._sub_function", "__defaults__", (False,)) def test_main_function(self): print(my

Assert an integer is within range

眉间皱痕 提交于 2020-03-14 05:45:46
问题 I am writing some unittests in python that are testing if I receive an integer. However sometimes this integer can be off by 1 or 2 and I don't really care. Essentially I want to be able to assert that the received integer is within a certain range, something like: self.assertBetween(998, 1000, my_integer) Is there an accepted way of doing this? Or will I have to do something like this: self.assertTrue(998 <= my_integer) self.assertTrue(my_integer <= 1000) EDIT The answers so far suggest:

Assert an integer is within range

妖精的绣舞 提交于 2020-03-14 05:44:55
问题 I am writing some unittests in python that are testing if I receive an integer. However sometimes this integer can be off by 1 or 2 and I don't really care. Essentially I want to be able to assert that the received integer is within a certain range, something like: self.assertBetween(998, 1000, my_integer) Is there an accepted way of doing this? Or will I have to do something like this: self.assertTrue(998 <= my_integer) self.assertTrue(my_integer <= 1000) EDIT The answers so far suggest:

Assert an integer is within range

偶尔善良 提交于 2020-03-14 05:44:26
问题 I am writing some unittests in python that are testing if I receive an integer. However sometimes this integer can be off by 1 or 2 and I don't really care. Essentially I want to be able to assert that the received integer is within a certain range, something like: self.assertBetween(998, 1000, my_integer) Is there an accepted way of doing this? Or will I have to do something like this: self.assertTrue(998 <= my_integer) self.assertTrue(my_integer <= 1000) EDIT The answers so far suggest:

Assert an integer is within range

喜你入骨 提交于 2020-03-14 05:44:08
问题 I am writing some unittests in python that are testing if I receive an integer. However sometimes this integer can be off by 1 or 2 and I don't really care. Essentially I want to be able to assert that the received integer is within a certain range, something like: self.assertBetween(998, 1000, my_integer) Is there an accepted way of doing this? Or will I have to do something like this: self.assertTrue(998 <= my_integer) self.assertTrue(my_integer <= 1000) EDIT The answers so far suggest:

Subprocesses not running with pytest Error Processing Test

旧城冷巷雨未停 提交于 2020-03-05 05:03:09
问题 I am currently converting my unittest setup over to pytest, everything is working however on certain tests I run a command line process in order to load code into the hardware I have attached via USB. this process works fine with unittest however when using pytest or nose2 I get the response ------------------------------------------------ Captured stderr call ------------------------------------------------- Error processing Test this happens just when my process begins to run? I get no