2

I started learning python as I developed a project about a year ago. Since then the project became somewhat of a (quite large) stable and useful tool for me. The project's arrangement is like so:

main.py
../functions/func1.py
../functions/func2.py
../functions/func3.py
../functions/func4.py
...
../functions/funcN.py

where the main.py file calls the rest of the functions sequentially.

The issue is that I did not write a single unit test for any of the functions. Not one. I did not pay much attention to testing since at first I was just learning and eventually it got out of hand.

I want to correct this and add the proper unit tests, the question is: which testing method should I use for my project?

I've seen many different methods described:

but I've no idea if one of those is more suited to something like my project than the rest.

2
  • 3
    Don't add tests for existing code just for their own sake, that's a waste of time. Add a test when you need to fix a bug (write a test that demonstrates it) or before you add new code. You've already tested the existing code manually, by using it. Commented Nov 5, 2014 at 15:03
  • @RemcoGerlich It could still help to improve code quality. Commented Nov 5, 2014 at 15:04

1 Answer 1

3

unittest which is now just unittest2 is already in python and the most standard, just start with that.

Think of nose as a set of extensions, use it when you want something not already in unittests, it's quite popular as well.

doctests puts unit tests into doc comments, I don't like it too much but use it if you want to.

mock is just a testing paradigm you should use when interacting with interfaces/objects is not trivial

tox runs tests under different python environments.

As an addition, integration tools like travis/jenkins allows you to run tox or sets of unit tests automatically, they're often used for multi-user projects, so everybody can see the test results on each commit.

Sign up to request clarification or add additional context in comments.

2 Comments

I think the point of doctests is that if you are going to have examples in your docstrings (often helpful), then they should be tested to make sure they stay up to date.
@RemcoGerlich Yeah, I often see that in research-orientated libraries, e.g. numpy/scipy, it's better when the underlying objects are simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.