python-unittest

unittest installation error Could not find a version that satisfies the requirement

会有一股神秘感。 提交于 2019-12-05 00:31:38
Could someone please help me with this error message: Could not find a version that satisfies the requirement unittest I installed the latest Python and PyCharm and try to install the package unittest but get the above error. So far my experience with Python is a bit like dll hell ... If you tried this: $ pip install unittest Collecting unittest Could not find a version that satisfies the requirement unittest (from versions: ) No matching distribution found for unittest It's normal to have an error because unittest is not a valid package on Pypi. The only distribuable is unittest2-0.0.0.tar.gz

Instantiate Python unittest.TestCase with arguments

…衆ロ難τιáo~ 提交于 2019-12-04 23:59:11
I would like to iterate over a list of items, and run an assertion on each of them. One example might be checking whether each number in a list is odd. TestCase : class TestOdd(unittest.TestCase): def runTest(self): """Assert that the item is odd""" self.assertTrue( NUMBER %2==1, "Number should be odd") Test suite : if __name__ == '__main__': suite = unittest.TestSuite() suite.addTest(TestOdd()) # I would like to have: # suite.addTest(TestOdd(1)) # suite.addTest(TestOdd(2)) # suite.addTest(TestOdd(3)) # ... unittest.main() How can I instantiate a TestOdd object with an argument - for example,

Is there any way to check with Python unittest assert if an iterable is not empty?

牧云@^-^@ 提交于 2019-12-04 22:11:49
After submitting queries to a service, I get a dictionary / a list back and I want to make sure it's not empty. I am on Python 2.7. I am surprised I don't see any assertEmpty method for the unittest.TestCase class instance. The existing alternatives such as: self.assertTrue(bool(d)) and self.assertNotEqual(d,{}) and self.assertGreater(len(d),0) just don't look right. Is this kind of method is missing in the Python unittest framework? If yes, what would be the most pythonic way to assert that an iterable is not empty? Empty lists/dicts evaluate to False, so self.assertTrue(d) gets the job done.

python mock and libraries that are not installed

二次信任 提交于 2019-12-04 20:35:43
问题 I am working on software for a robot, which is normally run on the Raspberry Pi. Let's consider the imports of two files: motor.py (runs the motors): from RPi import GPIO as gpio and client.py (communicates with the server and relays commands to the motors): from rpi.motor import Motor Both files are in a directory called rpi , which contains a __init__.py and a __main__.py . The RPi package cannot be installed on non-RPi devices. However, I still want to test the functionality of client.py .

How to sort unittest TestCases properly?

自作多情 提交于 2019-12-04 20:33:40
For instance, I want these to run in the order they appear in the file. import unittest class Test_MyTests(unittest.TestCase): def test_run_me_first(self): pass def test_2nd_run_me(self): pass def test_and_me_last(self): pass class Test_AnotherClass(unittest.TestCase): def test_first(self): pass def test_after_first(self): pass def test_de_last_ding(self): pass if __name__ == "__main__": unittest.main(verbosity=2) Running this gives test_after_first (__main__.Test_AnotherClass) ... ok test_de_last_ding (__main__.Test_AnotherClass) ... ok test_first (__main__.Test_AnotherClass) ... ok test_2nd

How to mock aiohttp.client.ClientSession.get async context manager

余生颓废 提交于 2019-12-04 11:00:34
问题 I have some troubles with mocking aiohttp.client.ClientSession.get context manager. I found some articles and here is one example that seems was working: article 1 So my code that I want to test: async_app.py import random from aiohttp.client import ClientSession async def get_random_photo_url(): while True: async with ClientSession() as session: async with session.get('random.photos') as resp: json = await resp.json() photos = json['photos'] if not photos: continue return random.choice

How to achieve TestNG like feature in Python Selenium or add multiple unit test in one test suite?

安稳与你 提交于 2019-12-04 09:41:31
问题 Suppose I have this two nosetest ExampleTest1.py and ExampleTest2.py ExampleTest1.py class ExampleTest1(TestBase): """ """ def testExampleTest1(self): ----- ----- if __name__ == "__main__": import nose nose.run() --------------- ExampleTest2.py class ExampleTest2(TestBase): """ """ def testExampleTest2(self): ----- ----- if __name__ == "__main__": import nose nose.run() Now I want to run such hundreds of test files from a single suite. I am looking something like TestNG feature like testng

Python unittest mock: Is it possible to mock the value of a method's default arguments at test time?

ぃ、小莉子 提交于 2019-12-04 02:53:27
问题 I have a method that accepts default arguments: def build_url(endpoint, host=settings.DEFAULT_HOST): return '{}{}'.format(host, endpoint) I have a test case that exercises this method: class BuildUrlTestCase(TestCase): def test_build_url(self): """ If host and endpoint are supplied result should be 'host/endpoint' """ result = build_url('/end', 'host') expected = 'host/end' self.assertEqual(result,expected) @patch('myapp.settings') def test_build_url_with_default(self, mock_settings): """ If

unittest.py doesn't play well with trace.py - why?

孤街醉人 提交于 2019-12-04 02:16:28
Wow. I found out tonight that Python unit tests written using the unittest module don't play well with coverage analysis under the trace module. Here's the simplest possible unit test, in foobar.py : import unittest class Tester(unittest.TestCase): def test_true(self): self.assertTrue(True) if __name__ == "__main__": unittest.main() If I run this with python foobar.py , I get this output: . ---------------------------------------------------------------------- Ran 1 test in 0.000s OK Great. Now I want to perform coverage testing as well, so I run it again with python -m trace --count -C .

PyUnit - How to unit test a method that runs into an infinite loop for some input?

我的未来我决定 提交于 2019-12-04 01:46:42
问题 A post in 2011 answered this question for NUnit: How to unit test a method that runs into an infinite loop for some input? Is there a similar TimeoutAttribute in PyUnit that I can use in the same fashion? I did some searching and found "Duration", but that didn't seem the same. 回答1: There doesn't appear there is anything in pyunit itself, but as a work around you can roll your own. Here is how to do it using the multiprocessing package. from functools import wraps from multiprocessing import