Hello,

I am writing my first WEB application in Python 3.4 using Flask. I mainly rely on the book "Flask Web Development" by Miguel Greenberg (ISBN: 978-1-449-37262-0) and the official documentation on Flask.

Now I try to write the first tests using unittest.TestCase and there are a few things that are not clear to me:

  1. In Miguel’s book on page 76, where he has several configuration classes. Why did he introduce TestingConfig.TESTING, DevelopemntConfig.DEBUG? I did not see these constants used anywhere.
  2. Do I understand correctly that in order to send HTTP GET / POST and other requests to the application, I must receive a self.app.test_client () test client?

UPD :

  1. If item 2 says "yes", you need to get a test client, then this means that in each setUp () method I have to create an instanse Flask, i.e. "app = myapplication.create_app ('testing')" followed by getting the test.client self.app.test_client (). Wouldn't creating app with create_app () in each setUp () method slow down the tests?

Perhaps there are more correct ways to write unit tests. Then I ask the experienced to share knowledge

UPD:

In my application, I use the following approach for creating instance flask: Factories & Extensions . That is, using the create_app function (configName)

  • "Won't this slow down the tests?" - speed is the last thing that should interest you in testing. In an amicable way - yes, the whole environment should be recreated from scratch. Unit tests are not written for web applications, they are written for individual classes. Tests for web applications is the level of system tests. - etki
  • @Etki: OK. There are such separate functions preceded by the Flask.route () decorator. How to test them without sending a request? - sys_dev
  • I don’t know, I’m not a python man |: But in general, everything is the same - a piece of code is taken, those categories of values ​​that should (or, on the contrary, should not) come are pushed into it, the output is checked that the output is correct. - etki

1 answer 1

  1. Strictly speaking, these "constants" may not be checked in tests - these are the parameters with which the application will be configured. However, the TESTING check is on page 83.

    TESTING mode will disable error trapping when processing requests and thereby help to get more correct information in tests.

    DEBUG mode reloads the local development server automatically and provides debugging tools.

  2. Yes. Usually the call goes to self.app, pointing to someapp.app.test_client () .

  3. Not. In setUp, you get a client for tests. You have already created an instance of the application ( someapp.app ).

  • I think you did not quite understand the essence of my question in Section 3. I updated the wording of my questions and added an explanation of how I create instanse Flask-a. - sys_dev
  • You do not need to create an instance of the application several times in tests. Create it once, in any convenient way. In the setUp method, you will receive a test client for an already created instance. This is indicated in the answer. - idle sign
  • That is, to remove from the setUp () for each test method in the setUp () for the entire TestCase class? I'm afraid that the test will be "fragile" - sys_dev
  • If in the test module you consider working with one application, perhaps it makes sense to create an application instance at the module level (right before the definitions of the test classes). - idle sign
  • By default, the local Flask web server starts up with a network port of 5000. Do I have to force this test before receiving a test client? - sys_dev