Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

Do you know any good example of Python unit testing code for testing an API?



share|improve this question

closed as not constructive by gnat, Glenn Nelson, GlenH7, MichaelT, jimwise Jan 31 '13 at 19:04

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

I haven't dived too deeply into the actual code, but it might be worth a look to see how Django's test framework handles it. Observing how it worked gave me a lot of direction while building a test framework for PHP/Symfony. –  user34530 Mar 11 '12 at 20:45

2 Answers 2

DocTest from the standard library will help you to some extent. It lets you put example invocations of your library (including code expected to break in some particular way) into your docstrings, and (as long as they are formatted like a session from the interactive interpreter), doctest will find them and use them as test cases - if the given code gets the right result, or throws the right exception, the test case passes.

share|improve this answer

If you're asking about functional testing (with database, fixtures etc.), then I don't see any specific problems with neither framework-specific tools nor more abstract python requests library. More of that, testing rest API is far more simpler than doing that with static content.

Simple example is here: (maybe I should take a time and finish that long post one day).

The main problems are rather with good quality of your factories to generate test-data, with parts of response you're going to test (you should be careful and concentrate and to run your predicates against test-subject, but not other pieces of data, otherwise you'll be trapped into "change one letter -> go fix 100 tests" hell).

If you're asking about unit-testing views, then it's no different then testing any other functions. I highly recommend my mockstar library for mocking away all side-effects declaratively, by the way.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.