3

I'm reading the following manual: http://flask.pocoo.org/docs/testing/

Flask has features to simulate requests and check results. The whole application is tested including DB.

It plays nicely with Python module unittest. But this seems to me like end-to-end or functional testing and other tools like Selenium (with Python binding) come to mind.

  1. Is unit-testing is only testing particular module/function/class completely isolated with mocks for external resources (DB, network, files)?

  2. Is it ok to use unit testing frameworks to drive functional\end-to-end tests?

2 Answers 2

3

Flask does not get in the way of Python/JavaScript testing tooling. For example, it is possible to handle all kinds of testing (I deliberately do not want to name them unit/functional/acceptance/integration tests whatever as there always be anybody who will find the classification wrong):

  • testing simple and/or pure functions or classes
  • as previous plus database (eg, SQLAlchemy) (this is usually more practical and effective than using mocks for ORM/data objects, albeit slower)
  • request-level testing (to test views / routes) - no real HTTP requests, but pretty close for the purpose
  • in-browser testing with real HTTP request, including testing JavaScript modules

For example, with pytest it's just a matter of conftest.py files and fixtures, which provide app, db, http server and Selenium driver (in two separate threads), etc. It is even possible to make certain tests faster by using transaction rollbacks instead of remaking database fixture by substituting (monkeypatching) commit method. During tests, Flask can serve special static folder (by registering test-time only blueprint) to test JavaScript modules using JavaScript-specific testing frameworks of choice.

I guess, other generic testing frameworks (I do not call them unit-testing!) can do the same with more or less boilerplate code and fixture organization.

1

Flask testing tooling shall serve you well as long as it relates to http communication.

As soon as you create an application, where browser takes significant role by rendering part of output, affecting what can and cannot be done, you should go for solutions like Selenium.

2
  • But what about using unittesting frameworks to manipulate functional testing tools (like Selenium)? Why this is accepted?
    – artvolk
    May 15, 2014 at 10:28
  • @artvolk Your question and my answer is about Flask testing framework. Sure, you can use other unittest stuff and extend it to use of Selenium - and these things are working. But this is not included in Flask testing toolset (as far as I understand it). Other toole which seem to me very nice and promising for this purpose is behave and behaving which is applying behave concepts to web app testing (using selenium) May 15, 2014 at 11:07

Your Answer

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

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