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.…
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…
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…
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…
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
│ └──…
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…
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…
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…
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
--…
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):
…
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…
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:
…
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…
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()…
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…