python-unittest

Overriding Python Unit Test module for custom output? [code updated]

…衆ロ難τιáo~ 提交于 2019-12-10 14:45:53
问题 Edit : solved! Will update shortly with a solution. Aim: I want to rewrite Python's UnitTest module so when I call it I get the following JSON output within the stdout stream. For example: { "errors":0, "failures":1, "ran":3, "skipped":0, "successful":2, "test_data":[ { "index":0, "result":1 }, { "index":1, "result":1 }, { "index":2, "result":-1 } ] } Problem: I've written some code to generate these test results, but I'm facing problems with writing code for the test_data attribute of the

Python unittest counting the number of tests

江枫思渺然 提交于 2019-12-10 11:24:58
问题 It's my first time playing around with Python's unittest for an assignment in school. I basically have a Circle Object, where I am using pyunit to make sure the data is stored properly. I noticed that Python only counts the number of methods as test cases as opposed to the number of assert statements. For instance I want to test that methods are working correctly, Python only counts the following as 2 tests, despite having 4 assert statements. It really caught me off guard, as with Java's

Accessing test status in pyunit tearDown

◇◆丶佛笑我妖孽 提交于 2019-12-10 11:09:44
问题 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? 回答1: 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):

Mocking Googleads for Unit Tests

浪子不回头ぞ 提交于 2019-12-10 10:56:48
问题 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

How to run Odoo tests unittest2?

怎甘沉沦 提交于 2019-12-10 10:36:39
问题 I tried running odoo tests using --test-enable, but it won't work. I have a couple of questions. According to the documentation Tests can only be run during module installation, what happens when we add functionality and then want to run tests? Is it possible to run tests from IDE like Pycharm ? 回答1: @aftab You need add log-level please see below. ./odoo.py -d <dbname> --test-enable --log-level=test and regarding you question, If you are making changes to installed modules and need to re test

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

南楼画角 提交于 2019-12-09 15:11:49
问题 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.

Command-line invocation of unittests from __main__ failing

拟墨画扇 提交于 2019-12-08 16:07:35
I am solving some exercises in Python and using unittest to automate some of the verification of my code. One program runs the single unittest just fine and it passes. The second gives the following error: $ python s1c6.py E ====================================================================== ERROR: s1c6 (unittest.loader._FailedTest) ---------------------------------------------------------------------- AttributeError: module '__main__' has no attribute 's1c6' ---------------------------------------------------------------------- Ran 1 test in 0.001s FAILED (errors=1) Here's the code for the

Django test runner fails in virtualenv on Ubuntu

末鹿安然 提交于 2019-12-08 14:56:02
问题 I've been struggling with a problem with the Django test runner installed in a Python virtualenv on Ubuntu 14.04. The same software runs fine on MacOS, and I think it was fine on an earlier version of Ubuntu. The failure message is: ImportError: '<test>' module incorrectly imported from '<base-env>/local/lib/python2.7/site-packages/<package-dir>'. Expected '<base-env>/lib/python2.7/site-packages/<package-dir>'. Is this module globally installed? And the full stack trace from the error is:

Mock Patches Appearing in the Wrong Order?

纵然是瞬间 提交于 2019-12-08 14:47:33
问题 I have a test module ( test.py ) which imports functions from another module ( keyboard.py ). keyboard.py def get_keys(keyList, timeStamped): return event.getKeys(keyList=keyList, timeStamped=timeStamped) def wait_keys(keyList, timeStamped): return event.waitKeys(keyList=keyList, timeStamped=timeStamped) test.py @mock.patch('keyboard.wait_keys') @mock.patch('keyboard.get_keys') def test_2(self, mock_waitKeys, mock_getKeys): mock_waitKeys.return_value = [['wait_keys!', 0.1]] mock_getKeys

Is it not possible to run test cases directly without putting them into a test suite first?

一个人想着一个人 提交于 2019-12-08 09:45:26
问题 Is it true that unittest can't run test cases directly without putting them into a test suite first? I saw it somewhere, but forgot where I saw it and whether it refers to python's unnittest module or Java's JUnit. Python's unittest offers two ways for implicitly creating and running test cases, but I am not sure whether they internally use test suite. For example, given a test case class: # test_module.py import unittest class WidgetTestCase(unittest.TestCase): def setUp(self): self.widget =