All Questions
Tagged with python unit-testing
67 questions
41
votes
6
answers
55k
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 ...
20
votes
8
answers
7k
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, ...
18
votes
1
answer
7k
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 ...
15
votes
3
answers
12k
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 ...
14
votes
3
answers
3k
views
Should I choose repeated code in unit test or test logic? Can I avoid both?
When writing unit tests, I feel that there is a trade-off between code repetition and test logic.
Example of my current (likely flawed) approach:
To test this function (overly simple function for ...
10
votes
1
answer
780
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'
...
9
votes
4
answers
7k
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 ...
8
votes
3
answers
760
views
Is having a switch to turn mocking on or off a code smell?
I have a method that looks like this:
def foobar(mock=False, **kwargs):
# ... snipped
foobar actually makes several calls to Amazon S3 and returns a composed result.
In order to make this ...
7
votes
2
answers
348
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 ...
7
votes
2
answers
779
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):
...
7
votes
1
answer
9k
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:
...
7
votes
1
answer
673
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(...
6
votes
2
answers
789
views
Writing functional tests for a legacy project
I am trying to add a couple of tests to a legacy C project. The project basically consists of a command line tool that prints something to stdout every time an event happens. Now, since writing unit ...
5
votes
3
answers
5k
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 ...
5
votes
3
answers
2k
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).
...