python-unittest

Running unittest Test Cases and Robot Framework Test Cases Together

烈酒焚心 提交于 2019-12-22 06:47:00
问题 Our group is evaluating Robot Test Framework for our QA group, not just for BDD, but also to possibly cover a lot of our regular functionality testing needs. It certainly is a compelling project. To what extent, if any, is Robot Framework based on xunit (unittest) architecture? I see that unittest asserts can be used, but I don't see that the RF testcases themselves are based on unittest.TestCase. Ideally, our organization would like to be able to be able to write Robot Framework tests, as

How to suppress ImportWarning in a python unittest script

拈花ヽ惹草 提交于 2019-12-22 04:53:55
问题 I am currently running a unittest script which successfully passes the various specified test with a nagging ImportWarning message in the console: ...../lib/python3.6/importlib/_bootstrap.py:219: ImportWarning: can't resolve package from __spec__ or __package__, falling back on __name__ and __path__ return f(*args, **kwds) .... ---------------------------------------------------------------------- Ran 7 tests in 1.950s OK The script is run with this main function: if __name__ == '__main__':

How do I mock a method that uses requests.get in my class?

一个人想着一个人 提交于 2019-12-21 22:59:21
问题 I'm attempting to create a few unit tests for my class. I want to mock these, so that I don't burn through my API quota running some of these tests. I have multiple test cases that will call the fetch method, and depending on the passed URL I'll get different results back. My example class looks like this: import requests class ExampleAPI(object): def fetch(self, url, params=None, key=None, token=None, **kwargs): return requests.get(url).json() # Returns a JSON string The tutorial I'm looking

python unittest assertRaises throws exception when assertRaises fails

家住魔仙堡 提交于 2019-12-21 12:24:12
问题 I've got code where assertRaises throws an exception when assertRaises fails. I thought that if assertRaises fails then the test would fail and I'd get a report at the end that says the test failed. I wasn't expecting the exception to be thrown. Below is my code. I'm I doing something wrong? I'm using Python 2.6.2. import unittest class myClass: def getName(self): raise myExcOne, "my exception one" #raise myExcTwo, "my exception two" #return "a" class myExcOne(Exception): "exception one"

How to sort unittest TestCases properly?

别来无恙 提交于 2019-12-21 06:29:26
问题 For instance, I want these to run in the order they appear in the file. import unittest class Test_MyTests(unittest.TestCase): def test_run_me_first(self): pass def test_2nd_run_me(self): pass def test_and_me_last(self): pass class Test_AnotherClass(unittest.TestCase): def test_first(self): pass def test_after_first(self): pass def test_de_last_ding(self): pass if __name__ == "__main__": unittest.main(verbosity=2) Running this gives test_after_first (__main__.Test_AnotherClass) ... ok test_de

nice html reports for pyunit

主宰稳场 提交于 2019-12-20 20:18:40
问题 Do you know a tool for creating nice html reports for pyunit? 回答1: I suggest the following: Run your tests using nose Create a nose plugin that outputs results as HTML. The nose example code has a simple HTML output plugin (https://raw.github.com/nose-devs/nose/master/examples/html_plugin/htmlplug.py). You can probably use that, at least as a starting point. Nose plug-in documentation: http://nose.readthedocs.org/en/latest/index.html Another option: Nose can output test results as NUnit

ImportError: cannot import name wraps

无人久伴 提交于 2019-12-20 16:48:13
问题 I'm using python 2.7.6 on Ubuntu 14.04.2 LTS. I'm using mock to mock some unittests and noticing when I import mock it fails importing wraps. Not sure if there's a different version of mock or six I should be using for it's import to work? Couldn't find any relevant answers and I'm not using virtual environments. mock module says it's compatible with python 2.7.x: https://pypi.python.org/pypi/mock mock==1.1.3 six==1.9.0 Python 2.7.6 (default, Mar 22 2014, 22:59:56) [GCC 4.8.2] on linux2 Type

Relative imports with unittest in Python

浪子不回头ぞ 提交于 2019-12-20 16:21:07
问题 I am trying to use Python unittest and relative imports, and I can't seem to figure it out. I know there are a lot of related questions, but none of them have helped so far. Sorry if this is repetitive, but I would really appreciate any help. I was trying to use the syntax from PEP 328 http://www.python.org/dev/peps/pep-0328/ but I must have something wrong. My directory structure is: project/ __init__.py main_program.py lib/ __init__.py lib_a lib_b tests/ __init__.py test_a test_b I run my

How do I test if a certain log message is logged in a Django test case?

故事扮演 提交于 2019-12-20 11:30:41
问题 I want to ensure that a certain condition in my code causes a log message to be written to the django log. How would I do this with the Django unit testing framework? Is there a place where I can check logged messages, similarly to how I can check sent emails? My unit test extends django.test.TestCase . 回答1: Using the mock module for mocking the logging module or the logger object. When you've done that, check the arguments with which the logging function is called. For example, if you code

addCleanup vs tearDown

折月煮酒 提交于 2019-12-20 11:03:46
问题 Recently, Ned Batchelder during his talk at PyCon 2016 noted: If you are using unittest to write your tests, definitely use addCleanup , it's much better than tearDown . Up until now, I've never used addCleanup() and got used to setUp() / tearDown() pair of methods for test "set up" and "tear down" phases. Why should I switch to addCleanup() instead of tearDown() ? It was also recently discussed in the Python unittest with Robert Collins podcast. 回答1: Per the addCleanup doc string: Cleanup