Questions tagged [python-unittest]

Python's standard library framework for testing.

Python standard library module for testing (docs).

3490 questions
26
votes
4 answers

How to test redirection In Flask with Python unittest?

I am currently trying to write some unit tests for my Flask application. In many of my view functions (such as my login), I redirect to a new page. So for example: @user.route('/login', methods=['GET', 'POST']) def login(): .... return…
Jason B
  • 7,097
  • 8
  • 38
  • 49
26
votes
3 answers

Why cant unittest.TestCases see my py.test fixtures?

I'm trying to use py.test's fixtures with my unit tests, in conjunction with unittest. I've put several fixtures in a conftest.py file at the top level of the project (as described here), decorated them with @pytest.fixture, and put their names as…
Will
  • 4,241
  • 4
  • 39
  • 48
25
votes
3 answers

Python - How can I assert a mock object was not called with specific arguments?

I realize unittest.mock objects now have an assert_not_called method available, but what I'm looking for is an assert_not_called_with. Is there anything like that? I looked on Google and didn't see anything, and when I tried just using…
Nathan Wailes
  • 9,872
  • 7
  • 57
  • 95
25
votes
3 answers

Python Mock Patch multiple methods in a class

Im trying to patch multiple methods in a class. Here is my simplified set up Hook.py is defined as class Hook(): def get_key(self): return "Key" def get_value(self): return "Value" HookTransfer.py defined as from Hook…
kvb
  • 625
  • 3
  • 8
  • 12
25
votes
7 answers

Run a specific unit test function inside PyCharm IDE 5.0.4

I am trying to use PyCharm for unit testing (with unittest), and am able to make it work: the test runner nicely shows the list of test cases and nested test functions. However, once the tests have been discovered, I cannot find any way to (re)run a…
Erwin Mayer
  • 18,076
  • 9
  • 88
  • 126
24
votes
4 answers

Recursive unittest discovery with python3 and without __init__.py files

I have project with the following directory structure: . ├── requirements.txt ├── main.py ├── tests ├── unit │ └── test_thing1.py │ └── test_thing2.py └── integration └── test_integration_thing1.py └──…
Timothée Jeannin
  • 9,652
  • 2
  • 56
  • 65
24
votes
1 answer

pytest dynamically generate test method

Hi How can i generate test method dynamically for a list or for number of files. Say I have file1,file2 and filen with input value in json. Now I need to run the same test for multiple values like below, class Test_File(unittest.TestCase): def…
Naggappan Ramukannan
  • 2,564
  • 9
  • 36
  • 59
24
votes
8 answers

PyCharm and unittest won't run

I have a problem with PyCharm 3.0.1 I can't run basic unittests. Here is my code : import unittest from MysqlServer import MysqlServer class MysqlServerTest(unittest.TestCase): def setUp(self): self.mysqlServer = MysqlServer("ip",…
drgn
  • 1,080
  • 1
  • 11
  • 21
23
votes
2 answers

How to resolve "ValueError: Empty module name"?

In my UnitTest directory, I have two files, mymath.py and test_mymath.py. mymath.py file: def add(a, b): return a + b def subtract(a, b): return a - b def multiply(a, b): return a * b def divide(numerator, denominator): return…
Shams Nahid
  • 6,239
  • 8
  • 28
  • 39
23
votes
1 answer

What is the difference between Python's unittest and unittest2 modules?

I currently work on some code that uses unittest2 module. I suspect this code was meant for the python2. Can one use python3 unittest as a drop in replacement for unittest2? What is the difference between the two?
22
votes
3 answers

How to capture the stdout/stderr of a unittest in a variable?

How to capture the stdout/stderr of a unittest in a variable? I need to capture the entire output output of the following unit test and send it to SQS. I have tried this: import unittest, io from contextlib import redirect_stdout,…
Bhushan
  • 18,329
  • 31
  • 104
  • 137
22
votes
2 answers

How to mock a property

I'm asking how to mock a class property in a unit test using Python 3. I've tried the following, which makes sense for me following the docs, but it doesn't work: foo.py: class Foo(): @property def bar(self): return 'foobar' def…
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
22
votes
4 answers

How do I patch an object so that all methods are mocked except one?

I have an entry point function call it main on an object that I would like to remain unmocked, since it calls several other methods on the object: class Thing(object): def main(self): self.alpha() self.bravo() def…
bbengfort
  • 5,254
  • 4
  • 44
  • 57
21
votes
2 answers

How to mock .query.filter_by() in Flask-SqlAlchemy

In brief When testing a model class in Flask-SqlAlchemy, how can we mock the method .query.filter_by() so as to return the list of mocked model objects? Full details Let's say we have a model class as below code from flask.ext.sqlalchemy import…
Nam G VU
  • 33,193
  • 69
  • 233
  • 372
21
votes
2 answers

Pytest - error vs fail

Im migrating from PyUnit to Pytest, and I found, that Pytest, unlike PyUnit, does not distinguish fails and errors in test report in quick report while running tests (where dots are printed). How to teach Pytest do do it? UPDATE Seems like it is…
Gill Bates
  • 14,330
  • 23
  • 70
  • 138