Questions tagged [python-unittest]

Python's standard library framework for testing.

Python standard library module for testing (docs).

3490 questions
1
vote
1 answer

Python Unit Test for FetchMany loop

I would like to unit test a piece of code where I am fetching results from a SQL server database and then loop through using fetchmany . I am not using fetchall as I expect the resultset to be huge. However, mocking it is causing an infinite loop as…
1
vote
0 answers

Patching python class constructor for unit testing

How can I test the constructor of my class, and subsequent objects created by it? I tried different combinations, and patching the __init__ function of MyClass, but I get different errors depending on what I try, for example __init__() should return…
Simen Russnes
  • 2,002
  • 2
  • 26
  • 56
1
vote
1 answer

How to mock a call to an API and return hard coded values?

I have an endpoint on my localhost to get time from different time zones. I type the time zone code in the URL and it shows the time for that time zone. This program uses an external API to get the time. I need to mock this call to the external API…
1
vote
0 answers

Why is my unit test returning False instead of True?

I'm running my first unit test on this function contained in a module called updater: import logging import subprocess logger = logging.getLogger(__name__) def update_oncourt(): return_code = subprocess.run(['perl',…
Jossy
  • 589
  • 2
  • 12
  • 36
1
vote
1 answer

How to mock an email confidential information in python

I have a method that returns a dictionary and that dictionary contains email specific confidential information like address,recipient, token etc. For sending an email, I need to access a rest URL and send the data along with confidential…
Rhea
  • 381
  • 1
  • 7
  • 22
1
vote
1 answer

Documenting member functions exclusively starting with "test_"

How can I document my tests, only my tests? I document my unittest with Sphinx. My setup is as followed: class MyTestWrapper1(unittest.Testcase) def test_general_setup() class MyTestWrapper2(MyTestWrapper1) def…
Cutton Eye
  • 3,207
  • 3
  • 20
  • 39
1
vote
1 answer

python if __name__ == "__main__": IndentationError: expected an indented block

I use PyCharm to right click Run 'hello ' or use shell to run python hello.py ,both return below error : if __name__ == "__main__": ^ IndentationError: expected an indented block below code is copied from…
Venus
  • 1,184
  • 2
  • 13
  • 32
1
vote
1 answer

How to test a named CheckConstraint or ValidationError is raised in unittests?

I am unittesting validations and checkconstraints on my model: class MyModel(models.Model): title = models.CharField() class Meta: models.CheckConstraint( check=~Q(title='Word'), name='title_cannot_be_word' …
alias51
  • 8,178
  • 22
  • 94
  • 166
1
vote
0 answers

Why does a ManyToMany relationship created in setUpTestData cause a duplicate key error?

I am using postgreSQL 12. I have the following models: class Publication(models.Model): title = models.CharField(max_length=30) class Article(models.Model): headline = models.CharField(max_length=100) publications =…
alias51
  • 8,178
  • 22
  • 94
  • 166
1
vote
1 answer

how to programmatically change a variable in a python package to run test

I have a project structure something like this. I want to write a test which programmatically change version variable in myPackage/version.py and write a test in tests/unit/test_release.py. ├── myPackage │   ├── __init__.py │   ├── root.py │   └──…
1
vote
1 answer

Coverage.py shows coverage of test files instead of Flask application files

I have come up with this issue, where by following the instructions found in the coverage.py page I end up getting the report on the test files themselves instead of the application files. I am referring to a Flask application, using unittest as a…
Pharaoh
  • 13
  • 3
1
vote
0 answers

Patching/Unpatching decorator using pytest

I am trying to patch a decorator. The testing framework that I am using is pytest. I am able to patch it successfully. However, the patch remains through out the test session, and thus I am not able to test the decorator itself. Here is a sample…
aakashgupta.0205
  • 647
  • 1
  • 8
  • 23
1
vote
1 answer

Python Unit-tests are running twice when run created test suite

[as I created test suite and generate a test report using HTMLTestRunner (Also modified little bit by me) single test run twice.] for that code (test suite) is: import os import time import unittest from xyz.base import log from builtins import…
Hollis HP
  • 11
  • 4
1
vote
0 answers

Deal with py2 incompatible syntax in tests

I have a unittest test case in my app which needs to be py2 and py3 compatible. class MyTest(TestCase): def test_pep3102(self): def fn(a, b, *, d): pass # do something # assert This test case works fine with py3…
Krimson
  • 7,386
  • 11
  • 60
  • 97
1
vote
1 answer

How to temporarily replace Python class method without mocking?

I have been wondering about this behaviour when replacing a class method I observe in Python 3.8 and concluded I do not understand it. I suspect it may have something to do with loosing the @classmethod decorator or something similar, but am at a…
vch
  • 131
  • 8