python-unittest

Python 3.3: DeprecationWarning when using nose.tools.assert_equals

坚强是说给别人听的谎言 提交于 2021-01-27 13:26:31
问题 I am using nosetest tools for asserting a python unittest: ... from nose.tools import assert_equals, assert_almost_equal class TestPolycircles(unittest.TestCase): def setUp(self): self.latitude = 32.074322 self.longitude = 34.792081 self.radius_meters = 100 self.number_of_vertices = 36 self.vertices = polycircles.circle(latitude=self.latitude, longitude=self.longitude, radius=self.radius_meters, number_of_vertices=self.number_of_vertices) def test_number_of_vertices(self): """Asserts that the

Python 3.3: DeprecationWarning when using nose.tools.assert_equals

孤者浪人 提交于 2021-01-27 13:21:42
问题 I am using nosetest tools for asserting a python unittest: ... from nose.tools import assert_equals, assert_almost_equal class TestPolycircles(unittest.TestCase): def setUp(self): self.latitude = 32.074322 self.longitude = 34.792081 self.radius_meters = 100 self.number_of_vertices = 36 self.vertices = polycircles.circle(latitude=self.latitude, longitude=self.longitude, radius=self.radius_meters, number_of_vertices=self.number_of_vertices) def test_number_of_vertices(self): """Asserts that the

Python unittest successfully asserts None is False

六月ゝ 毕业季﹏ 提交于 2021-01-22 06:32:58
问题 Why does assertFalse succeed on None ? import unittest class TestNoneIsFalse(unittest.TestCase): def test_none_is_false(self): self.assertFalse(None) Results: > python -m unittest temp . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK It seems as if this behaviour invites errors where a function does not always return a value. For example: def is_lower_than_5(x): if x < 5: return True elif x > 5: return False .... def test_5_is_not_lower_than_5

Python unittest successfully asserts None is False

淺唱寂寞╮ 提交于 2021-01-22 06:29:52
问题 Why does assertFalse succeed on None ? import unittest class TestNoneIsFalse(unittest.TestCase): def test_none_is_false(self): self.assertFalse(None) Results: > python -m unittest temp . ---------------------------------------------------------------------- Ran 1 test in 0.001s OK It seems as if this behaviour invites errors where a function does not always return a value. For example: def is_lower_than_5(x): if x < 5: return True elif x > 5: return False .... def test_5_is_not_lower_than_5

Python returns MagicMock object instead of return_value

强颜欢笑 提交于 2021-01-16 04:43:20
问题 I have a python file a.py which contains two classes A and B . class A(object): def method_a(self): return "Class A method a" class B(object): def method_b(self): a = A() print a.method_a() I would like to unittest method_b in class B by mocking A . Here is the content of the file testa.py for this purpose: import unittest import mock import a class TestB(unittest.TestCase): @mock.patch('a.A') def test_method_b(self, mock_a): mock_a.method_a.return_value = 'Mocked A' b = a.B() b.method_b() if

Testing Python Click Command Exceptions

 ̄綄美尐妖づ 提交于 2021-01-07 03:00:14
问题 I am trying to test the raising of exceptions by a command implemented with the Click package. This is my command: @click.option( '--bucket_name', ...) @click.option( '--group_id', ...) @click.option( '--artifact_id', ...) @click.option( '--version', ...) @click.option( '--artifact_dir', required=False, default='downloads/artifacts/', ...) @click.command() def download_artifacts( bucket_name, group_id, artifact_id, version, artifact_dir ): logger.info( f"bucket_name: {bucket_name}, " f"group

unittest unable to import class from pickle (AttributeError: Can't get attribute…)

落爺英雄遲暮 提交于 2021-01-05 11:35:33
问题 I need a unittest to load a previously saved class in a pickle. However, when I load the pickle in the unittest (out of unittest works), it raises the error: AttributeError: Can't get attribute 'Foo' on <module 'unittest. main ' from '...\unittest\ main .py'> Code example to save the class (I save this code in run_and_save_class.py ): from pickle import dump from pickle import load from pickle import HIGHEST_PROTOCOL class Foo(object): def __init__(self): self.bar = None self.file_out = ".

How to access the py.test capsys from inside a test?

天涯浪子 提交于 2020-12-10 00:17:52
问题 py.test documentations says that I should add capsys parameter to my test methods but in my case this doesn't seem to be possible. class testAll(unittest.TestCase): def setUp(self): self.cwd = os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe()))[0]) os.chdir(self.cwd) def execute(self, cmd, result=0): """ Helper method used by many other tests, that would prevent replicating too much code. """ # cmd = "%s > /dev/null 2>&1" % cmd ret = os.system(cmd) >> 8 self.assertEqual(ret

How to access the py.test capsys from inside a test?

北慕城南 提交于 2020-12-10 00:17:11
问题 py.test documentations says that I should add capsys parameter to my test methods but in my case this doesn't seem to be possible. class testAll(unittest.TestCase): def setUp(self): self.cwd = os.path.abspath(os.path.split(inspect.getfile(inspect.currentframe()))[0]) os.chdir(self.cwd) def execute(self, cmd, result=0): """ Helper method used by many other tests, that would prevent replicating too much code. """ # cmd = "%s > /dev/null 2>&1" % cmd ret = os.system(cmd) >> 8 self.assertEqual(ret

Python unit tests run function after all test

让人想犯罪 __ 提交于 2020-12-08 05:54:11
问题 I need to test smth on python via ssh. I don't want to make ssh connection for every test, because it is to long, I have written this: class TestCase(unittest.TestCase): client = None def setUp(self): if not hasattr(self.__class__, 'client') or self.__class__.client is None: self.__class__.client = paramiko.SSHClient() self.__class__.client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) self.__class__.client.connect(hostname=consts.get_host(), port=consts.get_port(), username=consts