python-unittest

Python unittest: how to run only part of a test file?

纵饮孤独 提交于 2019-11-30 10:17:01
问题 I have a test file that contains tests taking quite a lot of time (they send calculations to a cluster and wait for the result). All of these are in specific TestCase class. Since they take time and furthermore are not likely to break, I'd want to be able to choose whether this subset of tests does or doesn't run (the best way would be with a command-line argument, ie " ./tests.py --offline " or something like that), so I could run most of the tests often and quickly and the whole set once in

AttributeError: 'module' object has no attribute 'tests'

99封情书 提交于 2019-11-30 06:17:18
问题 I'm running this command: python manage.py test project.apps.app1.tests and it causes this error: AttributeError: 'module' object has no attribute 'tests' Below is my directory structure. I've also added app1 to my installed apps config. Traceback (most recent call last): File "manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line utility

patching a class yields “AttributeError: Mock object has no attribute” when accessing instance attributes

て烟熏妆下的殇ゞ 提交于 2019-11-30 05:42:06
The Problem Using mock.patch with autospec=True to patch a class is not preserving attributes of instances of that class. The Details I am trying to test a class Bar that instantiates an instance of class Foo as a Bar object attribute called foo . The Bar method under test is called bar ; it calls method foo of the Foo instance belonging to Bar . In testing this, I am mocking Foo , as I only want to test that Bar is accessing the correct Foo member: import unittest from mock import patch class Foo(object): def __init__(self): self.foo = 'foo' class Bar(object): def __init__(self): self.foo =

pytest -> How to use fixture return value in test method under a class

给你一囗甜甜゛ 提交于 2019-11-30 05:24:28
I have a fixture that returns a value like this: import pytest @pytest.yield_fixture(scope="module") def oneTimeSetUp(browser): print("Running one time setUp") if browser == 'firefox': driver = webdriver.Firefox() print("Running tests on FF") else: driver = webdriver.Chrome() print("Running tests on chrome") yield driver print("Running one time tearDown") This fixture gets the browser value from another fixture which is reading the command line option. Then I have a test class where I have more than one test methods and they all want to consume the same returned value driver to proceed the

Unable to run unittest's main function in ipython/jupyter notebook

眉间皱痕 提交于 2019-11-30 05:01:58
I am giving an example which throws an error in ipython/jupyter notebook, but runs fine as an individual script. import unittest class Samples(unittest.TestCase): def testToPow(self): pow3 = 3**3 assert pow3==27 if __name__ == '__main__': unittest.main() The error is below: --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-7-232db94ae8b2> in <module>() 8 9 if __name__ == '__main__': ---> 10 unittest.main() /usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest

Pycharm and unittest does not work

你离开我真会死。 提交于 2019-11-30 00:38:27
问题 I have a problem with PYCharm 3.0.1 I can't run basic unittests. Here is my code : import unittest from MysqlServer import MysqlServer class MysqlServerTest(unittest.TestCase): def setUp(self): self.mysqlServer = MysqlServer("ip", "username", "password", "db", port) def test_canConnect(self): self.mysqlServer.connect() self.fail() if __name__ == '__main__': unittest.main() Here is All the stuff pycharm give me Unable to attach test reporter to test framework or test framework quit

How can I check if a checkbox is checked in Selenium Python Webdriver?

这一生的挚爱 提交于 2019-11-29 23:18:31
I'm searching a week how check if a checkbox is checked in selenium webdriver with python, but I find only algoritms from JAVA. I readed the webdriver docs and it dont have a answer for that. Anyone have a solution? RocketDonkey There is a WebElement property called is_selected() , and for a check box this indicates whether or not it is checked. Therefore you can verify if it is checked/unchecked by doing something like this: driver.find_element_by_name('<check_box_name>').is_selected() or driver.find_element_by_id('<check_box_id>').is_selected() I remember having the same issue not being able

Python unittest.TestCase object has no attribute 'runTest'

别等时光非礼了梦想. 提交于 2019-11-29 11:13:04
问题 For the following code: import unittest class Test(unittest.TestCase): def test1(self): assert(True == True) if __name__ == "__main__": suite = unittest.TestSuite() suite.addTest(Test()) unittest.TextTestRunner().run(suite) Using Python 3 to execute it, the following error is raised: Traceback (most recent call last): File "test.py", line 10, in <module> unittest.TextTestRunner().run(suite) File "/usr/lib/python3.2/unittest/runner.py", line 168, in run test(result) File "/usr/lib/python3.2

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

为君一笑 提交于 2019-11-29 09:54:57
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 yole 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-unit-test-started-in-pycharm-or-not

How to stop all tests from inside a test or setUp using unittest?

做~自己de王妃 提交于 2019-11-29 06:09:05
问题 I'm extending the python 2.7 unittest framework to do some function testing. One of the things I would like to do is to stop all the tests from running inside of a test, and inside of a setUpClass() method. Sometimes if a test fails, the program is so broken it is no longer of any use to keep testing, so I want to stop the tests from running. I noticed that a TestResult has a shouldStop attribute, and a stop() method, but I'm not sure how to get access to that inside of a test. Does anyone