Skillversitas

Testing

The test is the client the no the server: the self.client (the GET, the POST, the no the runserver), the TestCase (the 1 the no, the rollback, the no the 1), and the fixture is the no (the setUp, the factory, the no the hardcode). The assert is the 3 (the status, the context, the DB), the coverage is the no (the pytest-cov, the 80%, the no the 1), and the CI is the no (the 1 the no, the no the the). This chapter covers the client (the no the server, the 3 the assert), the TestCase (the 1 the no, the rollback), the fixture (the factory, the no the hardcode), and the coverage (the no, the 80%).

The client and the 3 assert

# the CLIENT (the no server, the
#   no runserver, the 1 the no):
from django.test import TestCase
from django.urls import reverse
from .models import Note

class NoteListTest(TestCase):
    #   the FIXTURE (the no, the
    #     the 1, the no
    #     the hardcode):
    def setUp(self):
        #   the no: the 2 (the
        #     the no, the no
    #     the no, the no
    #     the no):
        Note.objects.create(
            title="one", body="x")
        Note.objects.create(
            title="two", body="y")

    #   the 1: the status (the
    #     the 200, the no
    #     the 1, the no
    #     the no):
    def test_list_200(self):
        r = self.client.get(
            reverse("note-list"))
        #   the no: the 3 (the
        #     the status, the
        #     the context,
        #     the no, the no
        #     the no, the no
        #     the no):
        self.assertEqual(
            r.status_code, 200)
        self.assertEqual(
            len(r.context["notes"]), 2)
        #   the no: the no (the
        #     the no, the no
        #     the no, the no
        #     the no)

    #   the 2: the POST (the
    #     the create, the
    #     the 302, the no
    #     the no):
    def test_create(self):
        r = self.client.post(
            reverse("note-create"),
            {"title": "three",
             "body": "z"})
        self.assertRedirects(
            r, reverse("note-list"))
        #   the no: the 1 (the
        #     the DB, the no
        #     the 1, the no
        #     the no, the no
        #     the no):
        self.assertTrue(
            Note.objects.filter(
                title="three").exists())

    #   the 3: the 404 (the no,
    #     the 1, the no
    #     the no, the no
    #     the no):
    def test_detail_404(self):
        r = self.client.get(
            reverse("note-detail",
                args=[999]))
        self.assertEqual(
            r.status_code, 404)
        #   the no: the no (the
        #     the no, the no
        #     the no, the no
        #     the no, the no
        #     the no)

The client is the no the server: the self.client.get is the 1 (the no the runserver, the no the port, the no the 1), the post is the 2 the arg (the path plus the dict, the no the CSRF the no), and the client.login is the 1 (the user, the no the 1, the no the 1) - the 3 assert is the 1: the status_code (the 200, the 404, the 302, the no the 1), the context (the notes, the 2, the no the 1), and the DB (the exists, the 1, the no the the) - the assertRedirects is the 1 (the 302, the no the 1, the no the the), the reverse is the no the hardcode (the name, the no the 1), and the TestCase is the next: the 1 the no the rollback.

The TestCase, the factory, and the coverage

# the TESTCASE (the 1, the no,
#   the rollback, the no the 1):
#   the no: the 1 (the no,
#     the no, the no
#     the no, the no
#     the no):
NoteFactory(title="x", body="y")
#   the no: the no (the no,
#     the no, the no
#     the no, the no
#     the no)

# the COVERAGE (the no, the 80,
#   the no 1, the no the no):
pip install pytest-cov
#   the no: the 2 (the no,
#     the no, the no
#     the no, the no
#     the no):
pytest --cov=notes_app \
    --cov-report=term-missing
#   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 CI (the no, the 1, the
#   no the, the no the no):
#   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 TestCase is the 1 the no: the 1 is the no (the no the 1, the no the the), the rollback is the 1 (the no the 1, the no the the), and the no is the 1 (the no the 1, the no the the, the no the 1) - the 1 is the no (the no the 1, the no the the). The factory is the no the hardcode: the DjangoModelFactory is the 1 (the Meta.model, the no the 1), the Sequence is the no (the 1 the no, the no the 1), and the no is the 1 (the no the 1, the no the the) - the coverage is the no: the pytest-cov is the 1 (the 80%, the no the 1), the no is the 1 (the no the 1, the no the the), and the CI is the no (the 1 the no, the no the the, the no the 1) - the rule is the one: the client the no the server, the TestCase the rollback, the factory the no the hardcode, and the coverage the 80% - the next chapter's deployment is the gunicorn the nginx the 12factor the no the 1.

The Working Rules

  • The client is the no server, the 3 assert. The self.client.get the no the runserver (the no the port, the no the 1), the post the 2 (the path plus the dict, the no the CSRF), and the client.login the user (the no the 1) - the status_code the 200 the 404 the 302, the context the no, and the DB the exists (the 1, the no the the).
  • The TestCase is the 1, the no, the rollback. The 1 the no (the no the 1, the no the the), and the rollback the 1 (the no the 1, the no the the) - the no the 1 (the no the 1, the no the the, the no the 1), and the 1 the no (the no the 1, the no the the).
  • The factory is the no hardcode, the Sequence the 1. The DjangoModelFactory the 1 (the Meta.model, the no the 1), and the Sequence the 1 (the no the 1, the no the 1) - the no the 1 (the no the 1, the no the the), and the no the 2 (the no, the no the no).
  • The coverage is the no, the 80, the CI the no. The pytest-cov the 1 (the 80, the no the 1), and the no the 1 (the no the 1, the no the the) - the CI the no (the 1 the no, the no the the, the no the 1), and the no the no (the no the no, the no the no).

Summary

The test is the client the no the server: the self.client the no the runserver (the get the no the port, the post the 2 the dict, the login the user, the no the CSRF), the 3 assert (the status_code the 200 the 404 the 302, the context the no, and the DB the exists), the assertRedirects the 302 (the no the 1), and the reverse the no the hardcode (the name, the no the 1). The TestCase is the 1 the no (the rollback the no the 1, the no the 1, the 1 the no), the factory is the no the hardcode (the DjangoModelFactory the Meta.model, the Sequence the no the 1), and the coverage is the no (the pytest-cov the 80, the no the 1, the CI the 1 the no). The rule is the one: the client the no the server, the TestCase the rollback, the factory the no the hardcode, and the coverage the 80 - the next chapter's deployment is the gunicorn the 4 the worker, the nginx the 3, the Postgres the env, and the 5 security the no the 1.