Questions tagged [python-unittest]

Python's standard library framework for testing.

Python standard library module for testing (docs).

3490 questions
39
votes
6 answers

numpy testing assert array NOT equal

We have numpy.testing.assert_array_equal to assert that two arrays are equal. But what is the best way to do numpy.testing.assert_array_not_equal, that is, to make sure that two arrays are NOT equal?
Hallgeir Wilhelmsen
  • 1,014
  • 1
  • 10
  • 18
38
votes
4 answers

Python unittest.TestCase object has no attribute 'runTest'

For the following code: import unittest class Test(unittest.TestCase): def test1(self): assert(True == True) if __name__ == "__main__": suite = unittest.TestSuite() suite.addTest(Test()) …
Jingguo Yao
  • 7,320
  • 6
  • 50
  • 63
38
votes
4 answers

Trying to implement python TestSuite

I have two test cases (two different files) that I want to run together in a Test Suite. I can get the tests to run just by running python "normally" but when I select to run a python-unit test it says 0 tests run. Right now I'm just trying to get…
avoliva
  • 3,181
  • 5
  • 23
  • 37
37
votes
5 answers

Assert an integer is within range

I am writing some unittests in python that are testing if I receive an integer. However sometimes this integer can be off by 1 or 2 and I don't really care. Essentially I want to be able to assert that the received integer is within a certain range,…
Yep_It's_Me
  • 4,494
  • 4
  • 43
  • 66
36
votes
4 answers

How do I run multiple Python test cases in a loop?

I am new to Python and trying to do something I do often in Ruby. Namely, iterating over a set of indices, using them as argument to function and comparing its results with an array of fixture outputs. So I wrote it up like I normally do in Ruby,…
picardo
  • 24,530
  • 33
  • 104
  • 151
36
votes
5 answers

How do I test if a certain log message is logged in a Django test case?

I want to ensure that a certain condition in my code causes a log message to be written to the django log. How would I do this with the Django unit testing framework? Is there a place where I can check logged messages, similarly to how I can check…
Krystian Cybulski
  • 10,789
  • 12
  • 67
  • 98
35
votes
2 answers

What is the correct order for actual and expected in pytest?

This question gives the order assertEqual(expected, actual), albeit for the unittest package. But Pycharm, with pytest, prints out "Expected:..." and "Actual..." based on the order actual==expected. This is confusing. What is the correct ordering…
Joshua Fox
  • 18,704
  • 23
  • 87
  • 147
35
votes
2 answers

Suppress print output in unittests

Edit: Please notice I'm using Python 2.6 (as tagged) Say I have the following: class Foo: def bar(self): print 'bar' return 7 And say I have the following unit test: import unittest class ut_Foo(unittest.TestCase): def…
asherbret
  • 5,439
  • 4
  • 38
  • 58
35
votes
2 answers

How do you mock patch a python class and get a new Mock object for each instantiation?

OK, I know this is mentioned in the manual, and probably has to do with side_effect and/or return_value, but a simple, direct example will help me immensely. I have: class ClassToPatch(): def __init__(self, *args): …
bavaza
  • 10,319
  • 10
  • 64
  • 103
34
votes
3 answers

Proper way to return mocked object using pytest.fixture

I'm trying to setup the target under test in @pytest.fixture and use it in all my tests in the module. I'm able to patch the test correctly, but after I add the @pytest.fixture to return the mock object and invoke the mocked object in other unit…
Bryan Cheng
  • 403
  • 1
  • 5
  • 11
34
votes
1 answer

unittest installation error Could not find a version that satisfies the requirement

Could someone please help me with this error message: Could not find a version that satisfies the requirement unittest I installed the latest Python and PyCharm and try to install the package unittest but get the above error. So far my experience…
cs0815
  • 16,751
  • 45
  • 136
  • 299
34
votes
5 answers

Mocking a subprocess call in Python

I have a method - run_script() - I would like to test. Specifically, I want to test that a call to subprocess.Popen occurs. It would be even better to test that subprocess.Popen is called with certain parameters. When I run the test however I get…
steve
  • 2,488
  • 5
  • 26
  • 39
32
votes
4 answers

How to mock aiohttp.client.ClientSession.get async context manager

I have some troubles with mocking aiohttp.client.ClientSession.get context manager. I found some articles and here is one example that seems was working: article 1 So my code that I want to test: async_app.py import random from aiohttp.client import…
32
votes
3 answers

How to use a pandas data frame in a unit test

I am developing a set of python scripts to pre-process a dataset then produce a series of machine learning models using scikit-learn. I would like to develop a set of unittests to check the data pre-processing functions, and would like to be able to…
tjb305
  • 2,580
  • 4
  • 15
  • 20
32
votes
1 answer

Python unitest - Use variables defined in module and class level setup functions, in tests

I am doing Python unit testing using nosetests to experiment with Python class and module fixtures, to have minimal setup across my tests. The problem is I am not sure how to use any variables defined in the setupUpModule and the setUpClass…
Amey
  • 8,470
  • 9
  • 44
  • 63