All Questions
Tagged with python unit-testing
53
questions
0
votes
1answer
105 views
How to write tests in TDD for downloading and unpacking a file?
So I want to write this function that downloads a file and unpacks it in python using TDD.
The function will look like this approximately
import urllib.request
import tarfile
def download_and_unpack(...
-1
votes
2answers
189 views
How do you unit test methods with hard/impossible inputs to hardcode?
I have the following problem. I have a method which takes two other custom objects to work. This objects are required by the method because they have the information that the method requires to ...
3
votes
2answers
144 views
How do you unit test functions split in smaller functions
The problem is the following, suppose we have this functions:
from PIL import Image
from magiclibrary import perform_some_operation, stack_images
def load_image(path: str):
if isfile(path):
...
0
votes
2answers
192 views
What if integration testing makes more sense than unit testing for a certain method?
I don't want to spam you with a ton of code, but please have a quick look at this boiler-plate method:
In this scenario let's say I have a ProcessingText.py file (class) that I finished its unit ...
1
vote
2answers
107 views
Unit testing a function that composes filters
Imagine there is a number of filter functions that all perform filtering of items in a list based on different criteria. Say there are 5 such filter functions. All unit tested
Now imagine there is ...
1
vote
1answer
110 views
Should I stub hidden dependencies in Python unit tests
Python is very specific language which gives developer huge flexibility.
So if I have class like this:
class Car:
def __init__(self, engine: Engine):
self._engine = engine # dependency ...
1
vote
1answer
138 views
How deep should I mock dependencies in unit tests
Consider the following function:
def get_api_status(api_client):
response = api_client.get('/status/')
return response.content
and the test for it:
def test_get_api_status():
...
16
votes
1answer
5k views
Is it possible/advisable to combine unit testing and integration testing?
I've built a Python script that consists of about 20 functions that extract data from MySQL via .sql files (I'm currently trying to learn SQLAlchemy ORM), do something to it and then put it back in ...
-5
votes
1answer
285 views
Is it possible to derive a test suite to achieve 100% path coverage? [closed]
Here is an example with the function:
Examining this I believe that it is possible to achieve 100% path coverage.
My understanding of path coverage is that you need to design test cases such that ...
0
votes
1answer
719 views
pep8 (D103) I need docstrings for my unit test functions too?
Perhaps im not defining my pytests right but im seeing this:
Does pep8 demand a docstring for each unit test function too? I cant find pep8 docs specific to this, wondering if pep8/flake8 is unit ...
7
votes
2answers
281 views
Should examples for functions be unit tests?
I'm writing a python function to replace all the non-alphanumeric characters in the keys of this dictionary with underscores.
To make sure it's working as expected as I don't have a ton of ...
0
votes
1answer
325 views
Which of these is a better practice to write Python unittest for CRUD operations of REST api?
I have a python class that has methods to perform the CRUD operations via REST api:
class my_class():
def get_obj(self,...) -> requests.Response:
res = requests.get(...);
...
2
votes
2answers
768 views
What is the difference between unit testing and handling exceptions
I have spent two full days now trying to understand the difference between unit testing and handling exception, but I can't get it.
Things I have understood (or I think I have):
Unit testing tests ...
3
votes
2answers
112 views
Best practices for testing settings file with many parameters in simulation code
I'm conflicted as to what is the best way to approach this problem.
I am writing a simulation in Python, which is parametrized by ~ 50 parameters. I have a JSON file where these parameters are set, ...
2
votes
1answer
98 views
How to increase test granularity without modifying the public interface
First of all, I'm aware that in python nothing is really private but let's assume that we can stick to using the leading underscore to indicate that something is private.
I have a module consisting ...
0
votes
3answers
315 views
Is using pytruth considered Pythonic?
I recently found Googles pytruth package. I am very used to pytest and I am wondering what the value of the way less commonly used pytruth is. The only reason why I didn't directly discard it, is that ...
3
votes
2answers
306 views
Does my class violate the Single Responsibility Principle in SOLID?
I want to ask:
Whether the Role class violates Single Responsibility Principle in SOLID ? I think deleteAccount() is not belong to Role class but Role class is way to extend code in the future
...
1
vote
2answers
105 views
Balancing function call overhead and testability in a code that is a part of the deep learning model training loop
I am currently implementing the transformer architecture for sequence to sequence problems. Key part of the model is the attention mechanism, which is basically a matrix multiplication, followed by a ...
2
votes
1answer
110 views
Best practices on testing a function based on third party service
I'm trying to figure out how to create unit tests for a function, which behavior is based on a third party service.
Suppose a function like this:
def sync_check():
delta_secs = 90
now = ...
2
votes
3answers
159 views
refactoring function to have a robust design
i am having a simple app example here:
say i have this piece of code which handles requests from user to get a list of books stored in a database.
from .handlers import all_books
from flask import ...
1
vote
1answer
203 views
How to organize my test functions?
I'm writing a software application of a few thousand lines of code (in Python), and in order to keep the whole thing together, slowly but certainly the need for unit tests (and later, other types of ...
24
votes
5answers
22k views
Why is unit testing private methods considered as bad practice?
Context:
I am currently working on a small project in Python. I commonly structure my classes with some public methods that are documented but mainly deal with the high level concepts (what a user of ...
1
vote
4answers
456 views
Unit testing Markov chain code
What are the best ways to unit test code that outputs random sequences satisfying specific conditions, such as Markov chains?
Let's be specific. There are two natural things to test:
That the ...
2
votes
3answers
241 views
Should I test the debug branch of my code in a unit test?
Lets say I have a function that gets a list of what files to send that are later send over a socket to some other pc. Now usually I keep track of what files have been sent and don't send these files ...
0
votes
2answers
566 views
Should I write integration test that test my code with real-network servers, before I start writing my code?
I am trying to write integration tests for a client-server app. The client app runs on a user's machine and connects to the server app running on a specific Windows server, network-serverA. The ...
-1
votes
2answers
2k views
Unit Testing a dictionary result
I often read that each unit tests should test one logical 'thing'.
What do I do when the return value of a function is a dictionary?
Usually I equality check the entire dictionary against an ...
3
votes
3answers
1k views
How should I unit test an impure function that calls a series of methods?
I wish to test the following method:
class MyClass(object):
...
def my_method(self, args):
... # Code which transforms args into arg1, arg2, ..., argN
self.A.other_method1(...
1
vote
1answer
2k views
Multiple tests per method, new workplace - Python
I am learning unit testing on the job, at a new job. The testing team is small, (1 other), and all of the tests are written in the following fashion.
class TestClass(unittest.TestCase):
def ...
13
votes
3answers
8k views
How to properly handle global parameters for unit testing in python?
We are implementing many algorithms which typically have lots of shared, publicly known and security-relevant parameters.
Currently, we simply use a class holding all the parameters and two ...
1
vote
1answer
249 views
Can't properly unit test most parts of a project, serious code smell?
The CMS we're developing is getting really messy. One of the failing parts is "unit testing".
To put it simple: what we call unit testing actually is something rather vague, closer to integration and ...
5
votes
1answer
789 views
How to think about a schedule that pulls from a database as objects
I am working on a personal project using Python. I have been using version control to the best of my abilities and if you would like to check it out and run the app https://github.com/CodeAmend/old-...
7
votes
1answer
584 views
How should the code for a program outputting to command line be tested/designed?
Imagine a program similar to this in Python:
import subprocess
class Example():
_cmd_args = (['ls', '-a', '/usr/bin/'], ['ls', '-al', '/usr/local/bin/'])
_default_args = 0
def init(...
5
votes
1answer
677 views
How to move Python doctest examples into another unit test framework?
Years ago AFAIK the mathematitician and software developer Tim Peters discovered that very often the documentation of APIs tends to get out of date over time during the software live cycle, because ...
10
votes
1answer
765 views
Unit testing for data munging pipelines made up of one-line functions
Reading Mary Rose Cook's Practical Introduction to Functional Programming, she give as an example of an anti-pattern
def format_bands(bands):
for band in bands:
band['country'] = 'Canada'
...
0
votes
2answers
907 views
Testing binary stream
I am building a binary file importer using Python. The specific file structure is defined in a specification. This looks like:
File
Map Block
String, Short Integer, Long Integer, String, Short ...
6
votes
1answer
8k views
how to test a generator with unittest? [closed]
I have programmed a small iterator in Python:
class anything():
def __init__(self):
self.i=1
def __iter__(self):
return self
def next(self):
if self.i>100:
...
3
votes
3answers
1k views
How much should I break up my unit tests?
I recently learned about unit tests and TDD and have started going back to add unit tests to my code (and thank god, it's proven that some things I've written are much more broken than I thought).
...
1
vote
1answer
729 views
Is there a cleaner way to test extras_require
I have code reviewed a piece of Python code, but to me it looks really ugly, hacky and complex for something that can be achieved very easily.
The code looks something similar to the following:
...
0
votes
1answer
269 views
How to write a good mock for testing
I code in Python and I'm starting to use the Mock library.
Although I've read through all of the documentation, something that I'm finding hard to understand is 'What makes a good mock'.
For example,...
4
votes
2answers
288 views
Unit Testing: How much more code? [duplicate]
I'm fairly new to unit testing. In school it's always been, "hey it works, onward!" But I've started to write professionally, and even at work that's been basically the mantra. However, I've started ...
-1
votes
3answers
3k views
Isolating unit tests in python
As a 30-year software developer, mostly in OO languages, but a newbie at python, I'm looking to find what is best practise for isolating unit tests in python.
Let's say I have the following, semi-...
3
votes
3answers
7k views
Is it possible to have setup and tear down methods run only for specific unittests?
I currently have a test class with a setUp method for creating a temp file and a tearDown method for deleting a temp file.
However, only some of the the test_ methods in that class require this ...
19
votes
8answers
6k views
What are good unit tests to cover the use case of rolling a die?
I'm trying to get to grips with unit testing.
Say we have a die which can has a default number of sides equal to 6 (but can be 4, 5 sided etc.):
import random
class Die():
def __init__(self, ...
5
votes
3answers
4k views
Writing a unit test for a platform dependent unit
I have a method to be tested which depends on the OS, what's the best approach for testing the method depending on the OS? Is it to test on every OS that I have I require? Is there a better approach ...
2
votes
3answers
2k views
How should I go about bringing this code under test?
I'm working on an open-source test framework.
90% of my codebase has good test coverage. My main problem area is the command-line entry point. This module began life as a very short script for ...
3
votes
1answer
2k views
How to keep unit tests independent?
I've read it at many places that unit tests should be independent. In my case I have a class that does data transformation. These steps must be done sequentially, otherwise they don't make sense. For ...
3
votes
2answers
248 views
Do I need to learn python first to understand the part 2 of the book Test Driven development?
It seems like Python is used as a coding language for part 2 of Kent Beck's book Test Driven Development. I have read the first part of that book and started appreciating the value of TDD . First part ...
8
votes
4answers
6k views
Behavior Driven Development and Unit Testing in Python [closed]
We might be interested in starting to incorporate a unit test suite to our project, which is coded in Python (and it uses Redis, PostgreSQL and some third-party libraries, if that bears into the ...
3
votes
1answer
2k views
scons and python unit tests best practices
I am using scons to build a large project containing a mix of C++ and Python. I would like scons to run Python unit tests either using nose or not. Currently, we have a long list of tests files and ...
1
vote
2answers
2k views
How to populate a private container for unit test?
I have a class that defines a private (well, __container to be exact since it is python) container. I am using the information within said container as part of the logic of what the class does and ...