Questions tagged [python-unittest]

Python's standard library framework for testing.

Python standard library module for testing (docs).

3490 questions
464
votes
18 answers

How do I run all Python unit tests in a directory?

I have a directory that contains my Python unit tests. Each unit test module is of the form test_*.py. I am attempting to make a file called all_test.py that will, you guessed it, run all files in the aforementioned test form and return the result.…
Stephen Cagle
  • 14,124
  • 16
  • 55
  • 86
390
votes
9 answers

Running a single test from unittest.TestCase via the command line

In our team, we define most test cases like this: One "framework" class ourtcfw.py: import unittest class OurTcFw(unittest.TestCase): def setUp: # Something # Other stuff that we want to use everywhere And a lot of test cases like…
Alois Mahdal
  • 10,763
  • 7
  • 51
  • 69
223
votes
4 answers

Python Mocking a function from an imported module

I want to understand how to @patch a function from an imported module. This is where I am so far. app/mocking.py: from app.my_module import get_user_name def test_method(): return get_user_name() if __name__ == "__main__": print "Starting…
nsfyn55
  • 14,875
  • 8
  • 50
  • 77
186
votes
8 answers

Disable individual Python unit tests temporarily

How can individual unit tests be temporarily disabled when using the unittest module in Python?
coelhudo
  • 4,710
  • 7
  • 38
  • 57
159
votes
3 answers

Python returns MagicMock object instead of return_value

I have a python file a.py which contains two classes A and B. class A(object): def method_a(self): return "Class A method a" class B(object): def method_b(self): a = A() print a.method_a() I would like to unittest…
Mehdi Jafarnia Jahromi
  • 2,017
  • 3
  • 15
  • 14
159
votes
4 answers

What is the difference between setUp() and setUpClass() in Python unittest?

What is the difference between setUp() and setUpClass() in the Python unittest framework? Why would setup be handled in one method over the other? I want to understand what part of setup is done in the setUp() and setUpClass() functions, as well as…
etja
  • 1,591
  • 2
  • 10
  • 6
130
votes
3 answers

Explain the "setUp" and "tearDown" Python methods used in test cases

Can anyone explain the use of Python's setUp and tearDown methods while writing test cases apart from that setUp is called immediately before calling the test method and tearDown is called immediately after it has been called?
NOOB
  • 2,717
  • 4
  • 22
  • 22
126
votes
11 answers

How to test Python 3.4 asyncio code?

What's the best way to write unit tests for code using the Python 3.4 asyncio library? Assume I want to test a TCP client (SocketConnection): import asyncio import unittest class TestSocketConnection(unittest.TestCase): def setUp(self): …
114
votes
2 answers

How to send requests with JSON in unit tests

I have code within a Flask application that uses JSONs in the request, and I can get the JSON object like so: Request = request.get_json() This has been working fine, however I am trying to create unit tests using Python's unittest module and I'm…
Sepehr Nazari
  • 3,744
  • 4
  • 12
  • 17
110
votes
12 answers

AttributeError: 'module' object has no attribute 'tests'

I'm running this command: python manage.py test project.apps.app1.tests and it causes this error: AttributeError: 'module' object has no attribute 'tests' Below is my directory structure. I've also added app1 to my installed apps…
Chris
  • 4,643
  • 6
  • 31
  • 49
87
votes
16 answers

Python unittest: how to run only part of a test file?

I have a test file that contains tests taking quite a lot of time (they send calculations to a cluster and wait for the result). All of these are in specific TestCase class. Since they take time and furthermore are not likely to break, I'd want to…
Gohu
  • 2,050
  • 2
  • 18
  • 17
86
votes
7 answers

How to assert that an iterable is not empty on Unittest?

After submitting queries to a service, I get a dictionary or a list back and I want to make sure it's not empty. I using Python 2.7. I am surprised of not having any assertEmpty method for the unittest.TestCase class instance. The existing…
Alex Tereshenkov
  • 3,340
  • 8
  • 36
  • 61
82
votes
7 answers

Python: Write unittest for console print

Function foo prints to console. I want to test the console print. How can I achieve this in python? Need to test this function, has NO return statement : def foo(inStr): print "hi"+inStr My test : def test_foo(): cmdProcess =…
sudhishkr
  • 3,318
  • 5
  • 33
  • 55
78
votes
2 answers

Python mock call_args_list unpacking tuples for assertion on arguments

I'm having some trouble dealing with the nested tuple which Mock.call_args_list returns. def test_foo(self): def foo(fn): fn('PASS and some other stuff') f = Mock() foo(f) foo(f) foo(f) for call in…
nackjicholson
  • 4,557
  • 4
  • 37
  • 35
74
votes
2 answers

ResourceWarning unclosed socket in Python 3 Unit Test

I'm modifying some code to be compatible between Python 2 and Python 3, but have observed a warning in unit test output. /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py:601: ResourceWarning: unclosed…
j12y
  • 2,112
  • 3
  • 17
  • 22
1
2 3
99 100