Questions tagged [python-unittest]

Python's standard library framework for testing.

Python standard library module for testing (docs).

3490 questions
74
votes
2 answers

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

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.…
Clandestine
  • 1,449
  • 1
  • 13
  • 30
64
votes
3 answers

How can I write unit tests against code that uses matplotlib?

I'm working on a python (2.7) program that produce a lot of different matplotlib figure (the data are not random). I'm willing to implement some test (using unittest) to be sure that the generated figures are correct. For instance, I store the…
pierre bonneel
  • 665
  • 1
  • 5
  • 8
62
votes
4 answers

How do I make coverage include not tested files?

I have just started writing some unit tests for a python project I have using unittest and coverage. I'm only currently testing a small proportion, but I am trying to work out the code coverage I run my tests and get the coverage using the…
user4591756
55
votes
9 answers

Python Unit Testing: Automatically Running the Debugger when a test fails

Is there a way to automatically start the debugger at the point at which a unittest fails? Right now I am just using pdb.set_trace() manually, but this is very tedious as I need to add it each time and take it out at the end. For Example: import…
tjb
  • 11,480
  • 9
  • 70
  • 91
55
votes
3 answers

Recursive unittest discover

I have a package with a directory "tests" in which I'm storing my unit tests. My package looks like: . ├── LICENSE ├── models │   └── __init__.py ├── README.md ├── requirements.txt ├── tc.py ├── tests │   ├── db │   │   └── test_employee.py │   └──…
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
55
votes
3 answers

Persist variable changes between tests in unittest?

How do I persist changes made within the same object inheriting from TestCase in unitttest? from unittest import TestCase, main as unittest_main class TestSimpleFoo(TestCase): foo = 'bar' def setUp(self): pass def…
A T
  • 13,008
  • 21
  • 97
  • 158
54
votes
3 answers

A way to output pyunit test name in setup()

Is there a way in python for a pyunit test to output the test it's currently running. Example: def setUp(self): log.debug("Test %s Started" % (testname)) def test_example(self): #do stuff def test_example2(self): #do other stuff def…
Brian O'Neill
  • 670
  • 1
  • 5
  • 6
46
votes
7 answers

Python unittest - Ran 0 tests in 0.000s

So I want to do this code Kata for practice. I want to implement the kata with tdd in separate files: The algorithm: # stringcalculator.py def Add(string): return 1 and the tests: # stringcalculator.spec.py from stringcalculator import…
MattSom
  • 2,097
  • 5
  • 31
  • 53
45
votes
5 answers

Python3.6 error: ModuleNotFoundError: No module named 'src'

I know similar questions have been asked before... But I had a quick doubt... I have been following this link: https://www.python-course.eu/python3_packages.php my code structure: my-project -- __init__.py -- src -- __init__.py --…
user3868051
  • 1,147
  • 2
  • 22
  • 43
45
votes
1 answer

Mock Patches Appearing in the Wrong Order?

I have a test module (test.py) which imports functions from another module (keyboard.py). keyboard.py def get_keys(keyList, timeStamped): return event.getKeys(keyList=keyList, timeStamped=timeStamped) def wait_keys(keyList, timeStamped): …
Louise
  • 1,063
  • 1
  • 9
  • 20
45
votes
1 answer

Patch over a function imported inside another function

In order to avoid a circular import, I've been forced to define a function that looks like: # do_something.py def do_it(): from .helpers import do_it_helper # do stuff Now I'd like to be able to test this function, with do_it_helper…
Wilduck
  • 13,822
  • 10
  • 58
  • 90
45
votes
8 answers

How to assert a dict contains another dict without assertDictContainsSubset in python?

I know assertDictContainsSubset can do this in python 2.7, but for some reason it's deprecated in python 3.2. So is there any way to assert a dict contains another one without assertDictContainsSubset? This seems not good: for item in dic2: …
JerryCai
  • 1,663
  • 4
  • 21
  • 36
44
votes
3 answers

Relative imports with unittest in Python

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…
J Jones
  • 3,060
  • 4
  • 26
  • 43
40
votes
3 answers

addCleanup vs tearDown

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()…
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
39
votes
2 answers

How do I use the unittest setUpClass method()?

I'm looking for some basic examples of the python 2.7 unittest setUpClass() method. I'm trying to test some class methods in my module, and I've gotten as far as: import unittest import sys import mymodule class…
urschrei
  • 25,123
  • 12
  • 43
  • 84