python-unittest

Mocking Googleads for Unit Tests

徘徊边缘 提交于 2019-12-06 09:47:59
I have a service that uses the googleads library in python, I want to unit test these functions but do not know how to go about mocking this aspect. When I used PHP and Zend Framework it was pretty easy to mock clients, as I could tell what was expected to be called, and mock what was returned, but I do not know how to do this here. Could you point to a good resource to do learn more about it? Here's some example code I'd like to test (get_account_timezone): from googleads import AdWordsClient from googleads.oauth2 import GoogleRefreshTokenClient from dateutil import tz class AdWords: ADWORDS

Accessing test status in pyunit tearDown

亡梦爱人 提交于 2019-12-06 09:36:53
I need to make a call to a web API after each pyunit test in a test suite passes or fails, so I basically need access to the test status in the tearDown method. But I can't find (or I've completely missed it) any documentation on who to access this data. Any ideas? Use TestResult . import unittest class TestFoo(unittest.TestCase): def test_ok(self): self.assertEqual(1+2, 3) def test_fail(self): self.assertEqual(1+2, 4) def test_error(self): 1/0 @unittest.skip('blah') def test_skip(self): self.assertEqual(42, 42) class MyResult(unittest.TextTestResult): def addError(self, test, err): self.call

if condition in setUp() ignore test

我的梦境 提交于 2019-12-06 04:56:46
in unittest python library, exists the functions setUp and tearDown for set variables and other things pre and post tests. how I can run or ignore a test with a condition in setUp ? You can call if cond: self.skipTest('reason') in setUp() . Instead of checking in setUp , use the skipIf decorator. @unittest.skipIf(not os.path.exists("somefile.txt"), "somefile.txt is missing") def test_thing_requiring_somefile(self): ... skipIf can also be used on the class, so you can skip all contained tests if the condition does not hold. @unittest.skipIf(not os.path.exists("somefile.txt"), "somefile.txt is

Print a different long description in nose tests along with test name python

蹲街弑〆低调 提交于 2019-12-06 02:29:10
I am using the command: nosetests test.py When this is run only the first line of the description gets printed. I want the whole description along with the test name. How do i do that? test.py file import unittests class TestClass(unittest.TestCase): def test_1(self): """this is a long description // continues on second line which does not get printed """ some code; self.assertTrue(True) def test_2(self): """this is another or different long description // continues on second line which does not get printed """ some code; self.assertTrue(True) if __name__ == '__main__': unittest.main()

Initializing MEDIA_ROOT before each Django Test

巧了我就是萌 提交于 2019-12-05 15:53:36
I want to my Django tests to create and modify media files. So, much like Django tests do with databases, I want to set up an empty MEDIA_ROOT folder before each test is run. I figured I'll create a temporary folder and point MEDIA_ROOT to it. However, I can't figure out where to put the code that does this. In this example , a special Runner is created. The runner sets up the media root and tears it down. Unfortunately, setup_test_environment is called once before the first test function is run, and not every time a test is run. I tried creating a FileSystemTestCase class that sets up the

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

雨燕双飞 提交于 2019-12-05 15:06:50
问题 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 ... 回答1: 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

Prevent custom assert from showing in traceback of python unittest

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 12:14:48
I'd like to add some custom assert methods to a TestCase. As a simple example, I've just put one inside the test class below. It works as expected, but when the output is generated the traceback includes the custom assert in the output. What is the step necessary to make it behave like assertEqual()? The code for assertEqual is in TestCase, but the actual line that raises the assertion does not appear in the traceback. What do I need to do to make test_something2's output look more like test_something1's? import unittest class TestCustomAssert(unittest.TestCase): def assertSomething(self, s):

Patch over a function imported inside another function

两盒软妹~` 提交于 2019-12-05 10:12:50
问题 In order to avoid a circular import, I've been forced to define a function that looks like: # do_something.py def do_it(): from .helpers import do_it_helper # do stuff Now I'd like to be able to test this function, with do_it_helper patched over. If the import were a top level import, class Test_do_it(unittest.TestCase): def test_do_it(self): with patch('do_something.do_it_helper') as helper_mock: helper_mock.return_value = 12 # test things would work fine. However, the code above gives me:

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

删除回忆录丶 提交于 2019-12-05 08:21:03
问题 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

How to suppress ImportWarning in a python unittest script

て烟熏妆下的殇ゞ 提交于 2019-12-05 04:06:08
I am currently running a unittest script which successfully passes the various specified test with a nagging ImportWarning message in the console: ...../lib/python3.6/importlib/_bootstrap.py:219: ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__ return f(*args, **kwds) .... ---------------------------------------------------------------------- Ran 7 tests in 1.950s OK The script is run with this main function: if __name__ == '__main__': unittest.main() I have read that warnings can be surpressed when the script is called like this: python