nose

How to run unittest discover from “python setup.py test”?

。_饼干妹妹 提交于 2019-11-28 03:53:25
I'm trying to figure out how to get python setup.py test to run the equivalent of python -m unittest discover . I don't want to use a run_tests.py script and I don't want to use any external test tools (like nose or py.test ). It's OK if the solution only works on python 2.7. In setup.py , I think I need to add something to the test_suite and/or test_loader fields in config, but I can't seem to find a combination that works correctly: config = { 'name': name, 'version': version, 'url': url, 'test_suite': '???', 'test_loader': '???', } Is this possible using only unittest built into python 2.7?

nose, unittest.TestCase and metaclass: auto-generated test_* methods not discovered

夙愿已清 提交于 2019-11-27 19:31:06
This is a follow-up question for unittest and metaclass: automatic test_* method generation : For this (fixed) unittest.TestCase layout: #!/usr/bin/env python import unittest class TestMaker(type): def __new__(cls, name, bases, attrs): callables = dict([ (meth_name, meth) for (meth_name, meth) in attrs.items() if meth_name.startswith('_test') ]) for meth_name, meth in callables.items(): assert callable(meth) _, _, testname = meth_name.partition('_test') # inject methods: test{testname}_v4,6(self) for suffix, arg in (('_false', False), ('_true', True)): testable_name = 'test{0}{1}'.format

Python imports for tests using nose - what is best practice for imports of modules above current package

让人想犯罪 __ 提交于 2019-11-27 17:56:44
This is a question which is asked frequently in different forms, and often obtains "lol you're not doing it properly" responses. Pretty sure that's because there's a common sense scenario people (including me) are trying to use as an implementation, and the solution is not obvious (if you've not done it before). Would accept an answer which "lets the fly out of the bottle". Given project/ __init__.py /code __init__.py sut.py /tests __init__.py test_sut.py Where tests_sut.py starts: import code.sut Running nosetests in the root dir leads to: ImportError: No module named code.sut Avenues

Nose unable to find tests in ubuntu

我是研究僧i 提交于 2019-11-27 11:25:59
Is there any reason why Nose wouldn't be able to find tests in Ubuntu 9.04? I'm using nose 0.11.1 with python 2.5.4. I can run tests only if I explicitly specify the filename. If I don't specify the filename it just says, 0 tests . The same project runs tests fine on my Mac, so I'm quite stumped! Some what related, if you're running tests off of a directory i.e nosetests ... tests/ where tests is the name of the folder with my tests, and have separate python test functions in one of the .py modules... Your functions have to start with 'test' for nosetests to recognize that as a test you want

Getting Python's unittest results in a tearDown() method

丶灬走出姿态 提交于 2019-11-27 10:55:06
Is it possible to get the results of a test (i.e. whether all assertions have passed) in a tearDown() method? I'm running Selenium scripts, and I'd like to do some reporting from inside tearDown(), however I don't know if this is possible. CAVEAT: I have no way of double checking the following theory at the moment, being away from a dev box. So this may be a shot in the dark. Perhaps you could check the return value of sys.exc_info() inside your tearDown() method, if it returns (None, None, None) , you know the test case succeeded. Otherwise, you could use returned tuple to interrogate the

Scrapy Unit Testing

久未见 提交于 2019-11-27 06:05:11
I'd like to implement some unit tests in a Scrapy (screen scraper/web crawler). Since a project is run through the "scrapy crawl" command I can run it through something like nose. Since scrapy is built on top of twisted can I use its unit testing framework Trial? If so, how? Otherwise I'd like to get nose working. Update: I've been talking on Scrapy-Users and I guess I am supposed to "build the Response in the test code, and then call the method with the response and assert that [I] get the expected items/requests in the output". I can't seem to get this to work though. I can build a unit-test

How to run unittest discover from “python setup.py test”?

雨燕双飞 提交于 2019-11-27 05:13:15
问题 I'm trying to figure out how to get python setup.py test to run the equivalent of python -m unittest discover . I don't want to use a run_tests.py script and I don't want to use any external test tools (like nose or py.test ). It's OK if the solution only works on python 2.7. In setup.py , I think I need to add something to the test_suite and/or test_loader fields in config, but I can't seem to find a combination that works correctly: config = { 'name': name, 'version': version, 'url': url,

Why python mock patch doesn't work?

纵然是瞬间 提交于 2019-11-27 05:02:55
I have two files spike.py class T1(object): def foo(self, afd): return "foo" def get_foo(self): return self.foo(1) def bar(): return "bar" test_spike.py: from unittest import TestCase import unittest from mock import patch, MagicMock from spike import T1, bar class TestStuff(TestCase): @patch('spike.T1.foo', MagicMock(return_value='patched')) def test_foo(self): foo = T1().get_foo() self.assertEqual('patched', foo) @patch('spike.bar') def test_bar(self, mock_obj): mock_obj.return_value = 'patched' bar = bar() self.assertEqual('patched', bar) if __name__ == "__main__": unittest.main() When I

Python Nose Import Error

冷暖自知 提交于 2019-11-27 02:59:48
I can't seem to get the nose testing framework to recognize modules beneath my test script in the file structure. I've set up the simplest example that demonstrates the problem. I'll explain it below. Here's the the package file structure: ./__init__.py ./foo.py ./tests ./__init__.py ./test_foo.py foo.py contains: def dumb_true(): return True tests/test_foo.py contains: import foo def test_foo(): assert foo.dumb_true() Both init .py files are empty If I run nosetests -vv in the main directory (where foo.py is), I get: Failure: ImportError (No module named foo) ... ERROR =======================

How to change the message in a Python AssertionError?

社会主义新天地 提交于 2019-11-26 22:41:05
问题 I'm writing per the following, in which I try to produce a decent error message when comparing two multiline blocks of Unicode text. The interior method that does the comparison raises an assertion, but the default explanation is useless to me I need to add something to code such as this below: def assert_long_strings_equal(one, other): lines_one = one.splitlines() lines_other = other.splitlines() for line1, line2 in zip(lines_one, lines_other): try: my_assert_equal(line1, line2) except