Questions tagged [python-unittest]

Python's standard library framework for testing.

Python standard library module for testing (docs).

3490 questions
21
votes
2 answers

Skip unittest if some-condition in SetUpClass fails

I was playing with pyUnit framework for unittest testing of my application. Is there any any way to skip all the tests in class if certain condition in setUpClass fails? Currently, I am setting up environment (creating resources, configuring global…
Mayur
  • 3,063
  • 10
  • 42
  • 55
20
votes
2 answers

Recommended PyUnit tutorials?

What is the best tutorial you know out there for getting started with PyUnit? (On Google I tend to find mostly fragmented hints and tricks and not much in terms of guides)
Samuel Lampa
  • 4,336
  • 5
  • 42
  • 63
20
votes
1 answer

testing postgres db python

I don't understand how to test my repositories. I want to be sure that I really saved object with all of it parameters into database, and when I execute my SQL statement I really received what I am supposed to. But, I cannot put "CREATE TABLE…
dgan
  • 1,349
  • 1
  • 15
  • 28
20
votes
3 answers

How to achieve assertDictEqual with assertSequenceEqual applied to values

I know that, when performing assertEqual on a dictionary, assertDictEqual is called. Similarly, assertEqual on a sequence will perform assertSequenceEqual. However, when assertDictEqual is comparing values, it appears not to make use of…
sapi
  • 9,944
  • 8
  • 41
  • 71
19
votes
1 answer

Unittesting with Pyspark: unclosed socket warnings

I want to do unittesting with PySpark. The tests itself work, however for each test I get ResourceWarning: unclosed and ResourceWarning: unclosed file <_io.BufferedWriter [...]> warnings and DeprecationWarnings regarding…
akoeltringer
  • 1,671
  • 3
  • 19
  • 34
19
votes
5 answers

Python coverage - exclude packages

I'm using the python coverage tool to run my unit test. As you can see from the result, it includes all the "site-packages". How can I exclude them from the report? I only want to show the report for project source code. (ctrp)…
user1187968
  • 7,154
  • 16
  • 81
  • 152
19
votes
3 answers

Unable to run unittest's main function in ipython/jupyter notebook

I am giving an example which throws an error in ipython/jupyter notebook, but runs fine as an individual script. import unittest class Samples(unittest.TestCase): def testToPow(self): pow3 = 3**3 assert pow3==27 if __name__ ==…
igauravsehrawat
  • 3,696
  • 3
  • 33
  • 46
19
votes
3 answers

For loop in unittest

Is there a way to tell the python unittest execute all assertion in a method and show all cases it fails, instead of stop at the first failed. class MyTestCase(TestCase): def test_a(self): with open('testcase.txt') as ifile: …
lucemia
  • 6,349
  • 5
  • 42
  • 75
18
votes
2 answers

What are the differences between unittest.mock, mock, mocker and pytest-mock?

I am new to Python development, I am writing test cases using pytest where I need to mock some behavior. Googling best mocking library for pytest, has only confused me. I have seen unittest.mock, mock, mocker and pytest-mock. Not really sure which…
Pritam Bohra
  • 3,912
  • 8
  • 41
  • 72
18
votes
2 answers

What is the difference between "from mock import patch" and "from unittest.mock import patch"?

What is the difference between these imports? from mock import patch vs from unittest.mock import patch Are they the same?
Woootiness
  • 1,882
  • 2
  • 15
  • 18
18
votes
2 answers

How do I run a single nosetest via setup.py in the python-active-directory module?

I am stubbornly trying to convert the Python module https://github.com/theatlantic/python-active-directory to Python 3. You can see my efforts here https://github.com/nbmorgan/python-active-directory/tree/master3. I have figured out the following…
Frobbit
  • 1,652
  • 17
  • 30
18
votes
3 answers

how to use assert_frame_equal in unittest

New to unittest package. I'm trying to verify the DataFrame returned by a function through the following code. Even though I hardcoded the inputs of assert_frame_equal to be equal (pd.DataFrame([0,0,0,0])), the unittest still fails. Anyone would…
Lisa
  • 4,126
  • 12
  • 42
  • 71
18
votes
3 answers

Passing arguments (for argparse) with unittest discover

foo is a Python project with deep directory nesting, including ~30 unittest files in various subdirectories. Within foo's setup.py, I've added a custom "test" command internally running python -m unittest discover foo '*test.py' Note that this…
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185
18
votes
3 answers

Python unittest mock: Is it possible to mock the value of a method's default arguments at test time?

I have a method that accepts default arguments: def build_url(endpoint, host=settings.DEFAULT_HOST): return '{}{}'.format(host, endpoint) I have a test case that exercises this method: class BuildUrlTestCase(TestCase): def…
nsfyn55
  • 14,875
  • 8
  • 50
  • 77
18
votes
2 answers

unittest - run the same test for a list of inputs and outputs

I have this test import unittest class TestName(unittest.TestCase): def setUp(self): self.name = "Bob" self.expected_name = "Bob" def test_name(self): # ... some operation over…
JuanPablo
  • 23,792
  • 39
  • 118
  • 164