python-unittest

How to check if python unit test started in PyCharm or not?

百般思念 提交于 2019-11-28 03:11:54
问题 Is there a way to check in a python unit test (or any other script) if it is executed inside the PyCharm IDE or not? I would like to do some special things in a unit test when it started locally, things I would not like to do when the whole thing is execute on the build server. Cheers 回答1: When running under PyCharm, the PYCHARM_HOSTED environment variable is defined. isRunningInPyCharm = "PYCHARM_HOSTED" in os.environ 来源: https://stackoverflow.com/questions/29777737/how-to-check-if-python

Skip unittest if some-condition in SetUpClass fails

喜你入骨 提交于 2019-11-27 21:14:02
问题 I was playing with pyUnit framework for unittest testing of my application. Is there any any way to skip all the tests in class if certain condition in setUpClass fails? Currently, I am setting up environment (creating resources, configuring global settings) in setUpClass. But, if any of these resource creation fails..I am raising exception. Instead of that I want to skip the whole test suite. 回答1: Got the answer: For those who gets stuck here- unittest can be skipped from setUpClass in

Python: Write unittest for console print

ε祈祈猫儿з 提交于 2019-11-27 20:12:27
问题 Function foo prints to console. I want to test the console print. How can I achieve this in python? Need to test this function, has NO return statement : def foo(inStr): print "hi"+inStr My test : def test_foo(): cmdProcess = subprocess.Popen(foo("test"), stdout=subprocess.PIPE) cmdOut = cmdProcess.communicate()[0] self.assertEquals("hitest", cmdOut) 回答1: You can easily capture standard output by just temporarily redirecting sys.stdout to a StringIO object, as follows: import StringIO import

How to send requests with JSONs in unit tests

不问归期 提交于 2019-11-27 18:54:33
I have code within a Flask application that uses JSONs in the request, and I can get the JSON object like so: Request = request.get_json() This has been working fine, however I am trying to create unit tests using Python's unittest module and I'm having difficulty finding a way to send a JSON with the request. response=self.app.post('/test_function', data=json.dumps(dict(foo = 'bar'))) This gives me: >>> request.get_data() '{"foo": "bar"}' >>> request.get_json() None Flask seems to have a JSON argument where you can set json=dict(foo='bar') within the post request, but I don't know how to do

Display python unittest results in nice, tabular form

我们两清 提交于 2019-11-27 18:12:14
I am writing a Pythonic tool which validates the correctness of a certain system. Each validation is written as a Python unittest , and the report looks like: test_exclude_list_not_empty (__main__.TestRepoLists) Assert the the exclude list is not empty ... ok test_include_list_not_empty (__main__.TestRepoLists) Assert the the include list is not empty ... ok test_repo_list_not_empty (__main__.TestRepoLists) Assert the the repo list is not empty ... ok In my opinion, this format is hard to read, especially for non-Pythonists. Is there any report generator that can generate a report in a nice,

How to test Python 3.4 asyncio code?

只谈情不闲聊 提交于 2019-11-27 17:07:56
What's the best way to write unit tests for code using the Python 3.4 asyncio library? Assume I want to test a TCP client ( SocketConnection ): import asyncio import unittest class TestSocketConnection(unittest.TestCase): def setUp(self): self.mock_server = MockServer("localhost", 1337) self.socket_connection = SocketConnection("localhost", 1337) @asyncio.coroutine def test_sends_handshake_after_connect(self): yield from self.socket_connection.connect() self.assertTrue(self.mock_server.received_handshake()) When running this test case with the default test runner, the test will always succeed

How can I simulate input to stdin for pyunit?

ε祈祈猫儿з 提交于 2019-11-27 14:38:56
I'm trying to test a function that takes input from stdin , which I'm currently testing with something like this: cat /usr/share/dict/words | ./spellchecker.py In the name of test automation, is there any way that pyunit can fake input to raw_input() ? Johnsyweb The short answer is to monkey patch raw_input() . There are some good examples in the answer to How to display the redirected stdin in Python? Here is a simple, trivial example using a lambda that throws away the prompt and returns what we want. System Under Test cat ./name_getter.py #!/usr/bin/env python class NameGetter(object): def

A way to output pyunit test name in setup()

感情迁移 提交于 2019-11-27 13:26:02
问题 Is there a way in python for a pyunit test to output the test it's currently running. Example: def setUp(self): log.debug("Test %s Started" % (testname)) def test_example(self): #do stuff def test_example2(self): #do other stuff def tearDown(self): log.debug("Test %s Finished" % (testname)) 回答1: You can use self._testMethodName . This is inherited from the unittest.TestCase parent class. def setUp(): print "In method", self._testMethodName 回答2: self.id().split('.')[-1] You can find the

Python Mocking a function from an imported module

巧了我就是萌 提交于 2019-11-27 10:46:36
I want to understand how to @patch a function from an imported module. This is where I am so far. app/mocking.py: from app.my_module import get_user_name def test_method(): return get_user_name() if __name__ == "__main__": print "Starting Program..." test_method() app/my_module/__init__.py: def get_user_name(): return "Unmocked User" test/mock-test.py: import unittest from app.mocking import test_method def mock_get_user(): return "Mocked This Silly" @patch('app.my_module.get_user_name') class MockingTestTestCase(unittest.TestCase): def test_mock_stubs(self, mock_method): mock_method.return

Python Unit Testing: Automatically Running the Debugger when a test fails

孤者浪人 提交于 2019-11-27 09:30:12
问题 Is there a way to automatically start the debugger at the point at which a unittest fails? Right now I am just using pdb.set_trace() manually, but this is very tedious as I need to add it each time and take it out at the end. For Example: import unittest class tests(unittest.TestCase): def setUp(self): pass def test_trigger_pdb(self): #this is the way I do it now try: assert 1==0 except AssertionError: import pdb pdb.set_trace() def test_no_trigger(self): #this is the way I would like to do