python-unittest

Disable individual Python unit tests temporarily

我的梦境 提交于 2020-01-11 15:08:14
问题 How can individual unit tests be temporarily disabled when using the unittest module in Python? 回答1: Individual test methods or classes can both be disabled using the unittest.skip decorator. @unittest.skip("reason for skipping") def test_foo(): print('This is foo test case.') @unittest.skip # no reason needed def test_bar(): print('This is bar test case.') For other options, see the docs for Skipping tests and expected failures. 回答2: You can use decorators to disable the test that can wrap

How to mock <ModelClass>.query.filter_by() in Flask-SqlAlchemy

狂风中的少年 提交于 2020-01-10 18:44:08
问题 In brief When testing a model class in Flask-SqlAlchemy, how can we mock the method .query.filter_by() so as to return the list of mocked model objects? Full details Let's say we have a model class as below code from flask.ext.sqlalchemy import SQLAlchemy db = SQLAlchemy() class SomeModel(db.Model): # more column mapping and methods go here Then in our Flask code we call SomeModel.query.filter_by(...) In our testing code, using Python unittest model with mocking, we want to mock the filter_by

Python: run a unittest.TestCase without calling unittest.main()?

我只是一个虾纸丫 提交于 2020-01-07 06:24:09
问题 I've written a small test suite in Python's unittest: class TestRepos(unittest.TestCase): @classmethod def setUpClass(cls): """Get repo lists from the svn server.""" ... def test_repo_list_not_empty(self): """Assert the the repo list is not empty""" self.assertTrue(len(TestRepoLists.all_repos)>0) def test_include_list_not_empty(self): """Assert the the include list is not empty""" self.assertTrue(len(TestRepoLists.svn_dirs)>0) ... if __name__ == '__main__': unittest.main(testRunner=xmlrunner

How to wrap correctly the unit testing diff?

假如想象 提交于 2020-01-06 14:54:35
问题 I am running python 3.6.4 , but some times the unit testing diff does not work as expected. For example, on the following there is a forced unit test error versus expected where the line wrapping behavior is not desired. import unittest class TestSemanticRules(unittest.TestCase): maxDiff = None def test_badWrapping(self): self.assertEqual( "1. Duplicated target language name defined in your grammar on: [@-1,63:87='Abstract Machine Language'<__ANON_3>,3:19]\n" "2. Duplicated master scope name

test argparse with unittest and mock

こ雲淡風輕ζ 提交于 2020-01-05 04:01:06
问题 I have a code sample: from argparse import ArgumentParser class Wrapper(object): def __init__(self): self.modules = set(['gpfs_server', 'gpfs_client']) self.services = set(['gpfs']) def get_opts(): parse_wrapper = Wrapper() parser = ArgumentParser() parser.add_argument("-i", "--info", dest="show_all", action = "store_true", default=False, help="Show supported services") args, unknown = parser.parse_known_args() args.sub_args = [] if unknown: if unknown[0] not in parse_wrapper.services:

Python unittest failure when results appear equal

江枫思渺然 提交于 2020-01-04 05:41:11
问题 I'm running a series of unit tests on an RPN calculator I've just put together. This calculator can mix integers, floats, and picas. I often need to be able to calculate using picas for some typesetting work I'm doing. For some reason, one of my unit tests is failing: Failure Traceback (most recent call last): File "/Users/andrew/Developer/pyRpn/test_rpn.py", line 112, in test_pica_subtracted_from_pica self.assertEqual(self.calc.rpn(['10p3', '2p1', '-']), ['8p2']) AssertionError: Lists differ

Python unit testing code which calls OS/Module level python functions

泪湿孤枕 提交于 2020-01-03 11:32:10
问题 I have a python module/script which does a few of these At various nested levels inside the script I take command line inputs, validate them, apply sensible defaults I also check if a few directories exist The above are just two examples. I am trying to find out what is the best "strategy" to test this. What I have done is that I have constructed wrapper functions around raw_input and os.path.exists in my module and then in my test I override these two functions to take input from my array

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

纵然是瞬间 提交于 2020-01-02 08:50:28
问题 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

Why cant unittest.TestCases see my py.test fixtures?

心已入冬 提交于 2020-01-02 02:21:27
问题 I'm trying to use py.test 's fixtures with my unit tests, in conjunction with unittest . I've put several fixtures in a conftest.py file at the top level of the project (as described here), decorated them with @pytest.fixture , and put their names as arguments to the test functions that require them. The fixtures are registering correctly, as shown by py.test --fixtures test_stuff.py , but when I run py.test , I get NameError: global name 'my_fixture' is not defined . This appears to only

How do I make coverage include not tested files?

你。 提交于 2019-12-31 08:28:33
问题 I have just started writing some unit tests for a python project I have using unittest and coverage . I'm only currently testing a small proportion, but I am trying to work out the code coverage I run my tests and get the coverage using the following python -m unittest discover -s tests/ coverage run -m unittest discover -s tests/ coverage report -m The problem I'm having is that coverage is telling I have 44% code coverage and is only counting the files that: were tested in the unit tests (i