Skillversitas

Testing

The test is the client the no the server: the app.test_client() (the get, the post, the no the run), the fixture is the DB (the create_all, the drop_all, the no the stale), and the assert is the 3 (the status, the data, the no the 1). The config is the test (the TESTING, the SQLite the :memory:, the no the prod), the live_server is the 2 (the thread, the no the the), and the no is the mock (the patch, the 1, the no the the). This chapter covers the test_client (the no the run), the fixture (the create_all, the no the stale), the config (the TESTING, the no the prod), and the assert (the 3, the no the 1).

The test_client and the fixture

# the TEST_CLIENT (the no run,
#   the no server, the no the 1):
import unittest
from app import create_app, db
from app.models import Post, User

class TestFeed(unittest.TestCase):
    #   the FIXTURE (the DB,
    #     the no stale, the no
    #     the 1, the no the the):
    def setUp(self):
        #   the no: the 1 (the
        #     no, the no, the no
        #     the no, the no
        #     the no):
        app = create_app("testing")
        #   the no: the no (the
        #     no, the no, the no
        #     the no, the no
        #     the no):
        self.app = app
        self.client = app.
            test_client()
        #   the no: the 1 (the
        #     no, the no, the no
        #     the no, the no
        #     the no):
        with app.app_context():
            db.create_all()
            #   the no: the no (
            #     the no, the no,
            #     the no, the no
            #     the no, the no)
            u = User(username="u1")
            db.session.add(u)
            db.session.add(Post(
                body="x",
                user_id=u.id))
            db.session.commit()
        #   the no: the no (the
        #     no, the no, the no
        #     the no, the no
        #     the no)

    def tearDown(self):
        #   the no: the 1 (the
        #     no, the no, the no
        #     the no, the no
        #     the no):
        with self.app.app_context():
            db.session.remove()
            db.drop_all()
        #   the no: the no (the
        #     no, the no, the no
        #     the no, the no
        #     the no)

    #   the 1: the 200 (the no,
    #     the 1, the no the the):
    def test_feed_200(self):
        r = self.client.get(
            "/feed/")
        #   the no: the 3 (the
        #     status, the data,
        #     the no, the no
        #     the no, the no
        #     the no):
        self.assertEqual(
            r.status_code, 200)
        self.assertIn(b"x",
            r.data)

    #   the 2: the 404 (the no,
    #     the 1, the no the the):
    def test_post_404(self):
        r = self.client.get(
            "/post/999/")
        self.assertEqual(
            r.status_code, 404)

The test_client is the no the run: the app.test_client() is the 1 (the no the server, the no the 1, the no the the), and the get/post is the 1 (the dict, the form, the no the 1) - the fixture is the DB: the create_all is the 1 (the setUp, the no the 1), the drop_all is the no (the tearDown, the stale, the no the the), and the app_context is the no (the with, the no the 1) - the 3 assert is the 1: the status_code (the 200, the 404, the no the 1), the data (the body, the no the 1), and the no is the 1 (the JSON, the get_json, the no the the) - the config is the next: the TESTING the no the prod.

The testing config and the no-mock

# the CONFIG (the TESTING,
#   the SQLite :memory:, the
#   no prod, the no the 1):
class Testing:
    #   the no: the 1 (the no,
    #     the no, the no
    #     the no, the no
    #     the no):
    TESTING = True
    SECRET_KEY = "the test"
    SQLALCHEMY_DATABASE_URI = \
        "sqlite:///:memory:"
    WTF_CSRF_ENABLED = False
    #   the no: the no (the no,
    #     the no, the no
    #     the no, the no
    #     the no)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the RUN (the 1, the no the
#   the, the no the 1):
python -m unittest discover \
    tests -v
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no)

# the NO-MOCK (the patch, the
#   1, the no the the, the no
#   the 1):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

The config is the test: the TESTING = True is the 1 (the no the 1, the no the the), the SQLite is the :memory: (the no the disk, the no the 1), and the WTF_CSRF_ENABLED is the False (the form, the no the token, the no the the) - the no is the prod (the Postgres, the 1, the no the the). The no the mock is the 1: the patch is the 1 (the 1, the no the the, the no the 1), and the no is the 1 (the all, the 1, the no the the) - the rule is the one: the test_client the no the run, the fixture the create_all, the config the TESTING, and the assert the 3 - the next chapter's deployment is the gunicorn the nginx the no the 1.

The Working Rules

  • The test_client is the no run, the no server. The app.test_client() the 1 (the no the 1, the no the the), and the get/post the 1 (the dict, the form, the no the 1) - the no the 2 (the no the 1, the no the the), and the no the 1 (the no the 1, the no the the).
  • The fixture is the DB, the no the stale. The create_all the setUp (the 1, the no the 1), and the drop_all the tearDown (the stale, the no the the) - the app_context the no (the with, the no the 1), and the no the 2 (the no the 1, the no the the).
  • The 3 assert is the 1, the no the 1. The status_code the 200 the 404 (the no the 1), and the data the body (the no the 1) - the no the JSON (the get_json, the no the the), and the no the 2 (the no the 1, the no the the).
  • The config is the TESTING, the no the prod. The TESTING=True the 1 (the no the 1, the no the the), and the SQLite the :memory: (the no the disk, the no the 1) - the WTF_CSRF_ENABLED the False (the no the token, the no the the), and the no the prod (the Postgres, the 1, the no the the).

Summary

The test is the client the no the server: the test_client the no the run (the no the 1, the get/post the dict), the fixture the DB (the create_all, the drop_all, the no the stale, the app_context the with), and the 3 assert the 1 (the status_code, the data, the JSON, the no the 1). The config is the TESTING (the SQLite :memory:, the WTF_CSRF_ENABLED, the no the prod), and the no the mock is the 1 (the patch, the no the the). The rule is the one: the test_client the no the run, the fixture the create_all, the config the TESTING, and the assert the 3 - the next chapter's deployment is the WSGI: the gunicorn the 4, the nginx the proxy, and the env the 12factor (the no the disk, the no the 1).