Questions tagged [python-unittest]

Python's standard library framework for testing.

1
vote
0answers
33 views

How to mock a python function inside another function

I have the following function in script.py: from mock import patch def outer(): def inner(): # something here return 5 return inner() + 2 print(outer()) # 7 To test the ...
0
votes
0answers
31 views

Python, Unittest + Pandas DataFrames: can this assertion test = a bool value (without getting a ValueError)?

I'm creating a class based only on inferences I gather by looking at pre-written Unittest test cases. Unittest will test my class and run assertion tests against its functions. I cannot change the ...
0
votes
2answers
25 views

Defined an ajax function in my urls.py file, but getting a 404 when invoking it in a test

I'm using Django and Python 3.7. I have this in my urls.py file urlpatterns = [ path(r'^ajax/calculate_taxes/$', post, name='calculate_taxes'), ] However, I'm getting a 404 when I try and ...
0
votes
1answer
14 views

How to use output of function present in pyqt5 class in another python file having different class?

I have created simple GUI using PYQT5 which asking user to provide server name . and want to use the user input in another python unittest script . I am very new in python world and trying to learn . ...
2
votes
1answer
38 views

Patching a function inside a package __init__ and use it within a module inside the same package

I am trying to patch an internal package function defined in the __init__.py that is being used by a function inside of a module inside of the same package. The problem that I am facing is that it ...
0
votes
0answers
21 views

AttributeError: 'DockerVolume' object has no attribute 'volume'

I have running the unittest , in that from another function variable it is used in a function.So while mocking that i'm getting error I'm getting error while executing the unittest for below code.For ...
0
votes
1answer
41 views

How to mock a pika connection for a different module?

I have a class that imports the following module: import pika import pickle from apscheduler.schedulers.background import BackgroundScheduler import time import logging class RabbitMQ(): def ...
3
votes
2answers
18 views

How do I mock a a file IO so that I can override the name attribute in a unit test?

I'm trying to write a unit test mock opening a file and passing it into a function that uses it to dump a JSON object into. How do I create a fake IO object that mimics the behavior of an open file ...
3
votes
1answer
41 views

Python file is read only after unit test failure

I am trying to create a unit test to test the following function: def decrypt_keys(self, enc_password): with open ('id_rsa','r') as privkeyfile: pem_key = privkeyfile.read() ...
1
vote
1answer
28 views

FIle Not Found - Python Unit Test

I have a python code to test. It's a simple code. The folder structure is as follows. . ├── code │ ├── conf.json │ ├── __init__.py │ └── a.py └── test ├── __init__.py ├── a_test.py ...
2
votes
2answers
38 views

Problem with unittest — receiving str object has no attribute 'get'

I am using sample code with unittest but I receive an error when I execute it -- 'str' object has no attribute 'get'. I searched Google but did not get an answer. from selenium import webdriver ...
0
votes
1answer
22 views

Is it alright to define a helping Class in a python test module?

I have a python module pikachu.py. For testing pikachu.py, I have test_pikachu.py. In test_pikachu.py, I have the test class TestPikachu (unittest.TestCase). Now to test a requirement for pikachu.py, ...
0
votes
2answers
24 views

How to send parameters to mock function?

I have two functions. Function function2() call function1(). Original function1() get one value from parameter and return one value. I want to test the function2() with mock function1(). But mock ...
0
votes
0answers
18 views

ModuleNotFoundError for pipenv packages installed from git on Windows

I'm trying to get a Python3 project that uses pipenv and has 2 packages referenced from github forks running on Windows (it works fine on a Mac and on Linux). I've cloned the repo and run pipenv ...
0
votes
0answers
27 views

How to fix AssertionError 200!=302

I got a registration view that registers new users, logs them in and then redirects them to home page. When I am testing this view using unittest I am getting an AssertionError 200!=302 views.py : ...
0
votes
0answers
26 views

Is it not possible to run test cases directly without putting them into a test suite first?

Is it true that unittest can't run test cases directly without putting them into a test suite first? I saw it somewhere, but forgot where I saw it and whether it refers to python's unnittest module or ...
0
votes
1answer
24 views

What happen behind `python3 -m unittest test_module.py`

In test_module.py, I define a subclass of TestCase consisting of several test methods. import unittest class abc(unittest.TestCase): def test1(): ... def test2(): ... I can run ...
0
votes
1answer
24 views

In Django how to mock an object method called by views.py during its import?

I am writing System Tests for my Django app, where I test the complete application via HTTP requests and mock its external dependencies' APIs. In views.py I have something like: from ...
2
votes
2answers
63 views

Authenticate Flask unit test client from another service (microservices architecture)?

Problem: So my problem is I have a Flask microservice want to implement the unit tests to it so when I start writing my test cases I found that I need to authenticate the unit test client because of ...
1
vote
1answer
65 views

Testing whether a Url is giving 500 error or not in Django [duplicate]

I want to test Urls whether am getting 500 error or not. In normal case where login is not required I get status_code 200 but where login is required, it gives me 302 error. So, how can one test ...
0
votes
2answers
9 views

How to use unittest to assert an assert?

Let's say I have this function: def to_upper(var): assert type(var) is str, 'The input for to_upper should be a string' return var.upper() And a file for unit testing using unittest: class ...
0
votes
0answers
29 views

2nd method in the pythonunit testing not working

I'm facing the very strange problem that python unit test on 2nd method not working. Below is the code from selenium import webdriver import unittest import time class PIM_config(unittest.TestCase):...
0
votes
0answers
31 views

How to mock the functions in a python script while executing the script using python command

I am trying to test a python script via python command using subprocess (Python27). Is there way to mock the functions being used in the script when I am testing like this? For example, example.py ...
0
votes
2answers
49 views

How to mock global variable evaluation for multiple tests?

I need to mock global variable inside a python script. This variable is initialized using a fairly expensive method which also throws when run outside target environment. I've found out how to ...
0
votes
0answers
4 views

Unittest executes only a blank result when ran in CMD prompt

I'm writing a Page Object Modele style Selenium project. Everything runs wonderfully in PyCharm, I get unittest to run and print results. However when I try to execute the code from command prompt and ...
2
votes
1answer
46 views

How to properly mock private members of a class

I am trying to write some unit tests for a method that depends on another private method. - As shown in the example below: def is_member_of(self, group_name): members = self.__get_group_members(...
0
votes
0answers
19 views

How to run debug on a single unittest method in VS code

I am trying to run a single unittest method in Visual Studio Code. However, every time I click on the Debug highlight above my test method name it will error when it starts to run ...
1
vote
1answer
37 views

Function called by a Celery task has no calls when patched in a unit test?

Consider the following tasks.py module (adapted from http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html#first-steps): import logging import sys from celery import ...
0
votes
1answer
20 views

How to mock internal function's side effect

HI I have a simple function. Which internally calls db but during local testing it can't connect DB so for that I have wrote specific exception and I want do side effect for same but it's not working. ...
0
votes
0answers
13 views

unittest: How does it actually execute my Test case?

Trying to understand how unittest works, I am looking into its source code and search for the lines which allow the execution of own test cases. I understood the things about setUp and tearDown and ...
0
votes
0answers
20 views

Creating new database for testing flask app models

I try to create testing database (postgresql) for my unittest. For this I created new configuration class at config.py file. class Configuration(object): DEBUG = True SECRET_KEY = os.environ....
0
votes
1answer
35 views

why “-m” and “unittest” when running unittest? [duplicate]

In the docs https://docs.python.org/3/library/unittest.html it suggests: python -m unittest tests/test_something.py whereas python tests/test_something.py seems to work just as well. Honestly, ...
0
votes
0answers
14 views

How to mock a function if there are both input and output file while writing unittest?

Im trying to write a test case for my function abc(input_file, length, output_file), where the function is reading the data from input_file and writing the output to the output_file I specify the ...
0
votes
1answer
34 views

how can i use the driver init in the setupModule

when i use unittest in python3, i tried like this: import unittest from selenium import webdriver def setupModule(): driver = webdriver.Firefox driver.maximize_window() driver.get('www....
0
votes
0answers
26 views

Flask unittest AssertionError: Popped wrong request context

I am using python 2.7, Flask and unittest, I am trying to mock a session to post a request when I am logged in. This is what do : def test_06_fill_form(self): with self.app as c: ...
0
votes
1answer
29 views

Python 2.6 Unittest assistance with parameters and argparse, how to solve?

I am trying to run a basic unit test on Python 2.6 that takes arguments with argparse. I am limited in my environment and cannot install any further libraries or use any modules for testing but ...
0
votes
1answer
134 views

How to patch imported function only in chosen class methods?

I have a class that has two methods. Both of those methods are using a request function imported from another module. How to patch request function only in one of these class methods? I've looked for ...
0
votes
1answer
37 views

Unit test fails on wtforms

When using pytest to do unittesting on posting datas to flask wtforms, the form's fileds' data is always '', which seems like the data is not properly "posted" to the forms, and it cause the form....
2
votes
1answer
21 views

Python3 Unittest: How to compare with MagicMocks Using Operators

Easiest thing is to start with an example... Example code to put under test: type1_instance1 = f1() type1_instance2 = f2() compareResult = type1_instance1 < type1_intstance2 if compareResult: ...
1
vote
1answer
34 views

Phantom tests after switching from unittest.TestCase to tf.test.TestCase

The following code: class BoxListOpsTest(unittest.TestCase): ...
0
votes
1answer
32 views

How do I test whether a Django QuerySet method was called?

Say I have the following: models.py: class FooQuerySet(models.QuerySet): def bar(self): return self.filter(...) class Foo(models.Model): ... objects = models.Manager....
0
votes
1answer
24 views

Python unittest changing variable seemingly outside of scope

I have the following Tic-Tac-Toe code: class TicTacToeBoard: board = [[0,0,0], [0,0,0], [0,0,0]] def printBoard(self): print(self.board) def putPiece(self, position1, position2): self.board[...
0
votes
1answer
29 views

Mocking a module function called by a static method does not work

Okay so I have a series of Python3 code that looks like this: foo.py from bar import ClassB class ClassA: @staticmethod def get_all(fn): something = ClassB(fn) return ...
0
votes
1answer
93 views

How to run Python's unittest.TestSuite() with -v flag?

I am using: Python 3.6.1 vanilla unittest framework I am aware that nose or py.test provide much more possibilities, than plain unittest. Though, the objective here is to stick with the current ...
2
votes
1answer
207 views

Python 2.7 Unittest check if warning is logged [duplicate]

I am trying to write a unit-test for my module that is written in Python 2.7, I can't migrate to 3.x right now. What i want is for this test do is to check if my module generates an warning logs, if ...
2
votes
1answer
27 views

Coverage shows too less percentage for Django test

i'm currently testing my django (2.1.0) application with some api-unit-tests. I used the django rest framework(3.9.0) to build a login. For this i use code like this: class LogoutTest(APITestCase): ...
0
votes
0answers
15 views

Mocking module and all sub-modules before import

I am dealing with a codebase that has a large number of modules which instantiate database connections, read from registry, etc when they are imported. (Code in a main function or in a class, but in ...
0
votes
0answers
12 views

Can we do cross browser testing using unittest framework?

I am using unittest framework for my script.I want to do cross browser testing with other browsers(other than chrome) also.How can we code in such a way that it runs for all browsers using unittest ...
1
vote
1answer
27 views

Python unittest: make AssertionError an error instead of a failure

Python 2.7. The unittest doc says: To make migrating existing test suites easier, unittest supports tests raising AssertionError to indicate test failure. However, it is recommended that you use ...
0
votes
0answers
20 views

How to mock at the descriptor level?

It appears uuid4 is not mocked within the Field descriptor's initializer, but it is within Foo.get_uid. Why is this? # file.py from uuid import uuid4 class Field: def __init__(self, default): ...

http://mssss.yulina-kosm.ru