Skip to main content

All Questions

Tagged with
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 ...
Serge Ballesta's user avatar
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, ...
Cybran's user avatar
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 ...
Jossy's user avatar
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 ...
netik's user avatar
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 ...
Jasper Braun's user avatar
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' ...
Max Flander's user avatar
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 ...
Juan Carlos Coto's user avatar
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 ...
Bon Ami's user avatar
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 ...
ADataGMan's user avatar
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): ...
Jorge Verdeguer Gómez's user avatar
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: ...
asgard's user avatar
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(...
enderland's user avatar
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 ...
user avatar
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 ...
Dean's user avatar
  • 449
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). ...
Dan Oberlam's user avatar

153050per page
close