python-unittest

Testing whether a Url is giving 500 error or not in Django [duplicate]

早过忘川 提交于 2019-12-11 06:38:01
问题 This question already has answers here : Django's self.client.login(…) does not work in unit tests (6 answers) Closed 8 months ago . I want to test Urls whether am getting 500 error or not. In normal case where login is not required I get status_code 200 but where login is required, it gives me 302 error. So, how can one test loginrequired and paramterized url in best way. Thank You So I am adding this because someone link that question into duplicate but it is not my answer and why it is not

Patching datetime.timedelta.total_seconds

萝らか妹 提交于 2019-12-11 06:36:38
问题 I write unit-tests for web application and i should change function waiting time TIME_TO_WAIT to test some modules. Example of code: import time from datetime import datetime as dt def function_under_test(): TIME_TO_WAIT = 300 start_time = dt.now() while True: if (dt.now() - start_time).total_seconds() > TIME_TO_WAIT: break time.sleep(1) I see a way to solve this problem with patch of datetime.timedelta.total_seconds(), but i don`t know, how do this correctly. Thanks. 回答1: As I wrote in the

AttributeError: TestSwitch instance has no attribute 'assertTrue'

社会主义新天地 提交于 2019-12-11 05:16:13
问题 I have a following pyunit test case code where I am collecting the result of the function (True or False) and using it to drive my assertion. However, I am getting the "no attribute" error for assertTrue. What is missing here? I am using python 2.7.8 and pyunit version of PyUnit-1.4.1-py2.7. The same code when run from the Eclipse (pydev plugin) from my Mac, it works fine. Only when I take this to my Linux box, it does throw below error. So to me it looks like some package incompatibility

How To Send Arguments to a UnitTest Without Global Variables

ε祈祈猫儿з 提交于 2019-12-11 03:13:26
问题 Problem Yes I know you shouldn't send arguments to a unittest but in this case I have to so that it can access a piece of hardware within the current framework. For reasons for which the detail is irrelevant, I can't use a simple board = myBoard.getBoard() method within the tests, otherwise I wouldn't be here asking you all. Horrible Attempt 1 class Test(object): @staticmethod def __call__(board): _board = board global _board logging.info('Entering a Unit Test') suite = unittest.TestLoader()

python mock default init argument of class

微笑、不失礼 提交于 2019-12-11 02:38:52
问题 I want to mock the default argument in a class constructor: class A (object): def __init__(self, connection=DefaultConnection()): self.connection = connection I want to mock DefaultConnection in my unittests, but it doesn't work when passed in as a default value. 回答1: You can use patch to patch the module, and then you can set the return value as a Mock. # --- a.py (in package path x.y) -- from c import DefaultConnection class A (object): def __init__(self, connection=DefaultConnection()):

Why does mocking 'open' and returning a FileNotFoundError raise AttributeError: __exit__?

半世苍凉 提交于 2019-12-11 02:26:42
问题 Testing by mocking open with a FileNotFoundError raises AttributeError: __exit__ . Why is this happening and what can I do to fix it? The following code opens a simple text file. If the file is missing it generates a default value. It has been checked by regular running and it appears to be working perfectly. so_main.py import os import so_config def load_savelocation(): path = os.path.join(so_config.ROOT, so_config.SAVELOCATION_FN) savelocation_path = os.path.normpath(path) try: with open

How to customize python unittest tests output?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 00:55:59
问题 I am working in the database domain. Few days back I started working on unit testing using python's most popular package i.e. unittest . I am totally new to python unittest. I want to test some database opreations like Add/update/delete with different database servers. I want the final test result as below sample result ------------------- ============== server 1: 20 passed 10 failed server 2: 10 passed 20 failed ============== and so on... My current class structure is Class DB_Test

How to assert operators < and >= are not implemented?

久未见 提交于 2019-12-11 00:19:19
问题 Initially I was not using the unittest framework, so to test that two objects of the same class are not comparable using the operators < and >= I did something like: try: o1 < o2 assert False except TypeError: pass after that, though, I decided to start using the unittest module, so I'm converting my tests to the way tests are written with the same module. I was trying to accomplish the equivalent thing as above with: self.assertRaises(TypeError, o1 < o2) but this does not quite work, because

“IOError: [Errno 35] Resource temporarily unavailable” with PhantomJS, python, selenium, unittest

寵の児 提交于 2019-12-10 18:11:16
问题 I'm having an issue with running unit tests using selenium and the PhantomJS driver. It seems to be related to resource contention for stderr/stdout in the PhantomJS process. The error is: $ python -m unittest selenium_failure.SeleniumTestCase [] [{u'timestamp': 1395857498698, u'message': u'{"log":{"version":"1.2","creator":{"name":"PhantomJS","version":"1.9.7"},"pages":[{"startedDateTime":"2014-03-26T18:11:38.347Z","id":"https://www.google.com/","title":"Google","pageTimings":{"onLoad":294}}

pytest - ModuleNotFoundError - python 3.6.4

本小妞迷上赌 提交于 2019-12-10 17:54:22
问题 I have a project with this layout: ├── MANIFEST.in ├── README.md ├── __init__.py ├── company │ ├── __init__.py │ ├── api │ │ ├── __init__.py │ │ ├── auth.py │ │ ├── debug.py │ │ ├── exceptions.py │ │ ├── reporting.py │ │ ├── rest.py │ │ ├── soap.py │ │ └── utils.py │ ├── jinjaEnvironment.py │ ├── sql │ │ ├── __init__.py │ │ ├── connection.py │ │ └── sql_helper.py │ └── templates │ ├── __init__.py │ ├── getUser.xml │ ├── rest_write_custom_field.xml │ └── setUser.xml ├── company.egg-info ├──