summaryrefslogtreecommitdiff
path: root/tutorial_10_django_tests/slides.md
blob: da97a0ee884063af6e2080083321cf188f80edf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
Slide 1
--------
Title Slide
** Writing Tests in Django**

Slide 2
--------

**Learning Objectives**

In this tutorial, we will learn to;
  - Write tests in django
  - Run tests in django

Slide 3
---------------

**System Requirements**
  - Ubuntu 16.10
  - Python 3.5 or higher version
  - python3.4-venv

Slide 4
---------------

**Pre-requisites**

In order to follow this tutorial, you need to know;
  - how to create models in django
  - how to use django shell
  - If not, see the relevant django tutorial on http://spoken-tutorial.org

Slide 5
-------

** Testing in Django**
 - Automated testing is very useful
 - We can validate our code during development
 - Testing is very complex, as we need to test models, views and forms.
 - In this tutorial we learn to write tests for models

Demonstration
-------------

 - Write tests in tests.py in blog app

        from django.test import TestCase

        # Create your tests here.

        from blog.models import Blog

        class BlogTestCase(TestCase):
            def setUp(self):
                Blog.objects.create(name='Blog1')

            def test_blog_created(self):
                blog = Blog.objects.get(id=1)
                name = 'Blog1'
                self.assertEqual(blog.name, name)

 - Run test

        python manage.py test blog

We will see test ran successfully as follow:

        Creating test database for alias 'default'...
        System check identified no issues (0 silenced).
        .
        ----------------------------------------------------------------------
        Ran 1 test in 0.002s

        OK
        Destroying test database for alias 'default'...


Demonstration
-------------
**Adding one more testcase**

        def test_was_created_recently(self):
            blog = Blog.objects.get(id=1)
            is_created_recently = True
            self.assertEqual(blog.was_created_recently(), is_created_recently)
 - Run
 - Output will be same as above except for following:

        Ran 2 tests in 0.003s


Demonstration
-------------
**Failing testcase**

 - Edit testcase

        def test_was_created_recently(self):
            blog = Blog.objects.get(id=1)
            is_created_recently = False # Change True to False
            self.assertEqual(blog.was_created_recently(), is_created_recently)

 - Run

 - Show assertion error in the following output:

        Creating test database for alias 'default'...
        System check identified no issues (0 silenced).
        .F
        ======================================================================
        FAIL: test_was_created_recently (blog.tests.BlogTestCase)
        ----------------------------------------------------------------------
        Traceback (most recent call last):
          File "/home/ttt/my-django/myproject/mysite/blog/tests.py", line 19, in test_was_created_recently
            self.assertEqual(blog.was_created_recently(), is_created_recently)
        AssertionError: True != False

        ----------------------------------------------------------------------
        Ran 2 tests in 0.003s

        FAILED (failures=1)
        Destroying test database for alias 'default'...


Demonstration
-------------
** Using assertTrue **

 - Edit testcase

        def test_was_created_recently(self):
            blog = Blog.objects.get(id=1)
            self.assertTrue(blog.was_created_recently())

 - Run
 - Output

        Ran 2 tests in 0.003s

        OK


Demonstration
--------------
 - Run individual test case

    - python manage.py test blog.tests.BlogTestCase.test_was_created_recently

 - Output

        Ran 1 test in 0.002s

        OK


Demonstration
--------------
 - Add testcase for Article model
 - Run
(For script creator: Can add this or give this as an assignment)

**Concluding Slides**