All Questions
Tagged with pythonunit-testing
67 questions
41votes
6answers
55kviews
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 ...
20votes
8answers
7kviews
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, ...
18votes
1answer
7kviews
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 ...
15votes
3answers
12kviews
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 ...
14votes
3answers
3kviews
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 ...
10votes
1answer
780views
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' ...
9votes
4answers
7kviews
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 ...
8votes
3answers
760views
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 ...
7votes
2answers
348views
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 ...
7votes
2answers
779views
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): ...
7votes
1answer
9kviews
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: ...
7votes
1answer
673views
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(...
6votes
2answers
789views
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 ...
5votes
3answers
5kviews
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 ...
5votes
3answers
2kviews
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). ...