Search code examples
pythondjangounit-testingdjango-unittest

Good approach/design to unittest django application


My django application is having a handler for each functionality (e.g. Salesinvoice handler for create/save/retrieve the invoice models with validation). These handlers are used in the views to perform the action user wanted (e.g. Create invoice).

  1. To ensure reliability of application, Will it be sufficient to unittest only the handlers? My idea in the above design is that if i need i can create a commandline interface using the handler instead of webinterface. Is this design of the application good to ensure reliably unittest django app? User request --> Views --> Handler (Unit test only the handler) --> Model

  2. *Is there any python/django library to automate the testing of views? * My goal is to ensure reliability of application by doing automated testing as much as possible.


Solution

  • I don't quite understand what you mean with a "handler". Is it a view? Or a separate function that accepts a request?

    Regarding the testing tools, you could simply use a library like requests to create requests and verify the results. There's also a builtin test client when using Django's internal testing framework.

    If you want to actually test the views in your browser, you can use Selenium or webdriver plus, which is a wrapper for Selenium that makes it easier to use.

    Potentially helpful links: