Questions tagged [python-unittest]

Python's standard library framework for testing.

Python standard library module for testing (docs).

3490 questions
31
votes
8 answers

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

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…
31
votes
3 answers

Display python unittest results in nice, tabular form

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…
Adam Matan
  • 128,757
  • 147
  • 397
  • 562
30
votes
4 answers

How to run Tox with Travis-CI

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…
Cerin
  • 60,957
  • 96
  • 316
  • 522
30
votes
4 answers

Testing Python Decorators?

I'm writing some unit tests for a Django project, and I was wondering if its possible (or necessary?) to test some of the decorators that I wrote for it. Here is an example of a decorator that I wrote: class login_required(object): def…
Jama22
  • 957
  • 1
  • 6
  • 20
30
votes
6 answers

How to stub time.sleep() in Python unit testing

I want to make a stub to prevent time.sleep(..) to sleep to improve the unit test execution time. What I have is: import time as orgtime class time(orgtime): '''Stub for time.''' _sleep_speed_factor = 1.0 @staticmethod def…
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
29
votes
2 answers

Assert that logging has been called with specific string

I'm trying to use unittest to test some functions of a SimpleXMLRPCServer I made. Togethere with Mock, I'm now trying to assert that a specific message has been logged when an if statement is reached, but I can't get it to work. I've tried…
Luca Giorgi
  • 910
  • 3
  • 14
  • 28
28
votes
5 answers

python - unittest - ImportError: Start directory is not importable

I'm following python unittest to make some test and use discover function to pack tests to a suite. However when I try to run the test with unittest, I get this error: Traceback (most recent call last): File "D:/Project/run_tests.py", line 12, in…
frank.hsueh
  • 281
  • 1
  • 3
  • 4
28
votes
5 answers

How to compare two files as part of unittest, while getting useful output in case of mismatch?

As part of some Python tests using the unittest framework, I need to compare two relatively short text files, where the one is a test output file and the other is a reference file. The immediate approach is: import…
EquipDev
  • 5,573
  • 10
  • 37
  • 63
28
votes
4 answers

Is it possible to skip setUp() for a specific test in python's unittest?

When I create a unittest.TestCase, I can define a setUp() function that will run before every test in that test case. Is it possible to skip the setUp() for a single specific test? It's possible that wanting to skip setUp() for a given test is not a…
7hi4g0
  • 3,729
  • 4
  • 22
  • 32
27
votes
5 answers

How can I simulate input to stdin for pyunit?

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…
bejar37
  • 335
  • 1
  • 3
  • 7
27
votes
5 answers

Change log level in unittest

I have the impression (but do not find the documentation for it) that unittest sets the logging level to WARNING for all loggers. I would like to: be able to specify the logging level for all loggers, from the command line (when running the tests)…
blueFast
  • 41,341
  • 63
  • 198
  • 344
27
votes
2 answers

Asserting that __init__ was called with right arguments

I'm using python mocks to assert that a particular object was created with the right arguments. This is how my code looks: class Installer: def __init__(foo, bar, version): # Init stuff pass def __enter__(self): …
Srikanth
  • 973
  • 2
  • 9
  • 19
26
votes
2 answers

Attempted relative import beyond toplevel package

Here is my folder structure: Mopy/ # no init.py ! bash/ __init__.py bash.py # <--- Edit: yep there is such a module too bass.py bosh/ __init__.py # contains from .. import bass bsa_files.py ... …
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
26
votes
3 answers

How to mock generators with mock.patch

I have gone through the page https://docs.python.org/3/library/unittest.mock-examples.html and i see that they have listed an example on how to mock generators I have a code where i call a generator to give me a set of values that i save as a…
akshitBhatia
  • 1,131
  • 5
  • 12
  • 20
26
votes
9 answers

ImportError: cannot import name wraps

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…
Michael
  • 1,577
  • 1
  • 18
  • 33