python-unittest

python mock and libraries that are not installed

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 12:45:52
I am working on software for a robot, which is normally run on the Raspberry Pi. Let's consider the imports of two files: motor.py (runs the motors): from RPi import GPIO as gpio and client.py (communicates with the server and relays commands to the motors): from rpi.motor import Motor Both files are in a directory called rpi , which contains a __init__.py and a __main__.py . The RPi package cannot be installed on non-RPi devices. However, I still want to test the functionality of client.py . import unittest from unittest import mock # Location A class TestClient(unittest.TestCase): # Location

How do I run multiple Python test cases in a loop?

拟墨画扇 提交于 2019-12-03 08:36:04
问题 I am new to Python and trying to do something I do often in Ruby. Namely, iterating over a set of indices, using them as argument to function and comparing its results with an array of fixture outputs. So I wrote it up like I normally do in Ruby, but this resulted in just one test case. def test_output(self): for i in range(1,11): .... self.assertEqual(fn(i),output[i]) I'm trying to get the test for every item in the range. How can I do that? 回答1: Using unittest you can show the difference

nice html reports for pyunit

ⅰ亾dé卋堺 提交于 2019-12-03 06:31:20
Do you know a tool for creating nice html reports for pyunit? codeape 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-compatible XML: nosetests --with-xunit . This will produce a nostests.xml file in the current directory

ImportError: cannot import name wraps

泪湿孤枕 提交于 2019-12-03 04:31:32
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 "help", "copyright", "credits" or "license" for more information. >>> from mock import Mock Traceback

Relative imports with unittest in Python

自作多情 提交于 2019-12-03 04:20:26
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 tests using: python -m unittest test_module1 test_module2 test_a needs to import both lib/lib_a and main

How do I run multiple Python test cases in a loop?

♀尐吖头ヾ 提交于 2019-12-03 01:29:22
I am new to Python and trying to do something I do often in Ruby. Namely, iterating over a set of indices, using them as argument to function and comparing its results with an array of fixture outputs. So I wrote it up like I normally do in Ruby, but this resulted in just one test case. def test_output(self): for i in range(1,11): .... self.assertEqual(fn(i),output[i]) I'm trying to get the test for every item in the range. How can I do that? Using unittest you can show the difference between two sequences all in one test case. seq1 = range(1, 11) seq2 = (fn(j) for j in seq1)

addCleanup vs tearDown

巧了我就是萌 提交于 2019-12-03 01:08:13
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. Per the addCleanup doc string : Cleanup items are called even if setUp fails (unlike tearDown) addCleanup can be used to register multiple functions,

how to specify test specific setup and teardown in python unittest

情到浓时终转凉″ 提交于 2019-12-03 00:51:38
问题 I want to create unittest test with two different set up and tearDown methon in same class with two different test. each test will use its specific setUp and tearDown method in python unittest framework. could anyone help me. class processtestCase(unittest.TestCase): print "start the process test cases " def setUp1(self): unittest.TestCase.setUp(self) def test_test1(self): "test Functinality" def tearDown1(self): unittest.TestCase.tearDown(self) def setUp2(self): unittest.TestCase.setUp2(self

Is there a way to hook Django's unittest into PyUnit in eclipse?

我怕爱的太早我们不能终老 提交于 2019-12-02 23:35:21
I've been working on a Django project for the past few weeks now, and it's been fairly smooth sailing. I use Django's unittest library to test everything, and the result show up in the console. However, after doing some research, it looks like pydev provides facilities for PyUnit . I use Eclipse, and I thought I would just just be able to pipe the unit tests into PyUnit, but I haven't had any luck in either that or finding documentation to do so. How do I have my unit tests run in the PyUnit view in eclipse? Currently, I run all of my test within eclipse using the following: manage.py test

How to run Tox with Travis-CI

假如想象 提交于 2019-12-02 18:09:32
How do you test different Python versions with Tox from within Travis-CI ? I have a tox.ini : [tox] envlist = py{27,33,34,35} recreate = True [testenv] basepython = py27: python2.7 py33: python3.3 py34: python3.4 py35: python3.5 deps = -r{toxinidir}/pip-requirements.txt -r{toxinidir}/pip-requirements-test.txt commands = py.test which runs my Python unittests in several Python versions and works perfectly. I want to setup a build in Travis-CI to automatically run this when I push changes to Github, so I have a .travis.yml : language: python python: - "2.7" - "3.3" - "3.4" - "3.5" install: - pip