create account

[Python #15] [Django #8] 开发完,需测试 by june0620

View this thread on: hive.blogpeakd.comecency.com
· @june0620 ·
$8.30
[Python #15] [Django #8] 开发完,需测试
pixabay
https://cdn.pixabay.com/photo/2019/05/14/17/07/web-development-4202909_1280.png

开发完功能应该要做什么?继续开发下一个功能?不~ 应需要测试,测试是必须的。
上期开发的搜索功能也需要我测试。 

幸好python提供一个库叫unittest,django也具备了基于unittest的库。
django默认创建的文件里也存在着一个叫`tests.py`的文件。
在这里我需要添加些测试代码。 

##### 组合用例 
最麻烦也最重要的就是组合测试用例。 
三个选项 tags, titles, texts 均为list,并且除了 tags 的小写英文局限性外没有多少限制。
因此测试用例会很多,不多想也能列出大概50多个用例。
心想,我这个小功能不值得我准备50个用例来测试,POI 有点低?先简单过滤一些吧。

|Name|value1|value2|value3|value4
|---|---|---|---|---
|TAGS|搜索不到的值 | 空list | list 大小为1的正常值 | 空字符串
|TITLES| 搜索不到的值 | 空list | list 大小为2的正常值 | 特殊字符与数字
|TEXTS| 搜索不到的值 | 空list | list 大小为3的正常值 | 空字符串

用 PICT 来组合上述用例应该会有不错的结果。
<sub>예전 글 [[IT] MS PICT 的 Pairwise 测试](https://steemit.com/hive-101145/@june0620/it-ms-pict-pairwise)</sub> 👇
```
TAGS: no_results,,valid_1,empty
TITLES: no_results,,valid_2,special and num
TEXTS: no_results,,valid_3,empty

if [tags] = "no_results" then [titles] = "no_results" and [texts] = "no_results" else [titles] <> "no_results" and [texts] <> "no_results";
if [tags] = "empty" then [texts] <> "empty";
if [texts] = "empty" then [tags] <> "empty";
```

生成了最佳的组合12个,12个可以接受,正好~ 哈哈 👇

![](https://cdn.steemitimages.com/DQmNf9cFB49Gv5VzNYPt1tKNXaRdRDWWfkhbG5JkZds6sSE/image.png) 

##### 编写测试代码
先简单学一下unittest基本用法。 
1. 首先在 `tests.py`创建 SearchTest 类并继承 TestCase。 
2. 测试用例函数必须以 test开头。 
```
from django.test import TestCase
from .services import Search


class SearchTest(TestCase):

    def test_01_empty_tag(self):
        query = {
            'tags': [''],
            'titles': ['#2'],
            'texts': ['사랑', 'Pairwise', '区块链']
        }
        s = Search(query)
        self.assert_text_contains(query, s.search_posts())

    def test_02_valid_title(self):
        query = {
            'tags': [],
            'titles': ['#2'],
            'texts': ['']
        }
        s = Search(query)
        self.assert_text_contains(query, s.search_posts())

    def test_03_valid_all(self):
        query = {
            'tags': ['kr'],
            'titles': ['#2', '검색'],
            'texts': ['사랑', 'pairwise', '블록체인']
        }
        s = Search(query)
        self.assert_text_contains(query, s.search_posts())

    def test_04_valid_body(self):
        query = {
            'tags': [],
            'titles': [],
            'texts': ['pixabay', '区块链', 'Pairwise']
        }
        s = Search(query)
        self.assert_text_contains(query, s.search_posts())

    def test_05_valid_tag_and_title(self):
        query = {
            'tags': ['cn'],
            'titles': ['#2'],
            'texts': []
        }
        s = Search(query)
        self.assert_text_contains(query, s.search_posts())

    def test_06_valid_all(self):
        query = {
            'tags': [''],
            'titles': [],
            'texts': []
        }
        s = Search(query)
        self.assertTrue(s.search_posts())

    def test_07_valid_title(self):
        query = {
            'tags': [],
            'titles': ['selenium', 'java #2'],
            'texts': []
        }
        s = Search(query)
        self.assert_text_contains(query, s.search_posts())

    def test_08_no_results(self):
        query = {
            'tags': ['nononononono'],
            'titles': ['ttttt'],
            'texts': ['aaaaaa']
        }
        s = Search(query)
        self.assertFalse(s.search_posts())

    def test_09_valid_tags_and_titles(self):
        query = {
            'tags': ['kr'],
            'titles': ['#2', '검색'],
            'texts': ['']
        }
        s = Search(query)
        self.assert_text_contains(query, s.search_posts())

    def test_10_valid_tags_and_titles(self):
        query = {
            'tags': [''],
            'titles': ['#2', '검색'],
            'texts': []
        }
        s = Search(query)
        self.assert_text_contains(query, s.search_posts())

    def test_11_valid_tags(self):
        query = {
            'tags': ['kr'],
            'titles': [],
            'texts': ['']
        }
        s = Search(query)
        self.assert_text_contains(query, s.search_posts())

    def test_12_empty_list_all(self):
        query = {
            'tags': [],
            'titles': [],
            'texts': []
        }
        s = Search(query)
        self.assertTrue(s.search_posts())

    def assert_text_contains(self, query: dict, blogs: list):
        self.assertTrue(blogs)
        all_q = query['tags'] + query['titles'] + query['texts']
        for blog in blogs:
            comm = blog['comment']
            cont = comm['json_metadata'] + comm['body'] + comm['title']
            any_one = any(query.lower() in cont.lower() for query in all_q)
            self.assertTrue(any_one)



```

##### 执行测试
用`python workspace/my_app/manage.py test my_app`执行测试即可。
初次执行的时候失败太多,差点崩溃。花点时间改了改漏洞,幸好现在都OK了。
![](https://cdn.steemitimages.com/DQmUcrMY4iNzDeT13w33tre3kad6yNGrkBejcEJRahBGDdv/image.png)

每当功能上有改动时可以用这个用例测试了。

***

**[Cookie 😅]**
Python 3.7.4
Django 2.2.4
steem-python 1.0.1
goorm IDE 1.3

参考文章: 
https://docs.python.org/ko/3/library/unittest.html
https://docs.djangoproject.com/ko/3.1/topics/testing/
https://docs.djangoproject.com/ko/3.1/intro/tutorial05/
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 12 others
properties (23)
authorjune0620
permlinkpython-15-django-8
categoryhive-105017
json_metadata{"app":"peakd/2020.08.3","format":"markdown","tags":["cn","dblog","whalepower","palnet","python","steempython","goorm","dev","django"],"users":["june0620"],"links":["https://steemit.com/hive-101145/@june0620/it-ms-pict-pairwise","https://docs.python.org/ko/3/library/unittest.html","https://docs.djangoproject.com/ko/3.1/topics/testing/","https://docs.djangoproject.com/ko/3.1/intro/tutorial05/"],"image":["https://cdn.pixabay.com/photo/2019/05/14/17/07/web-development-4202909_1280.png","https://cdn.steemitimages.com/DQmNf9cFB49Gv5VzNYPt1tKNXaRdRDWWfkhbG5JkZds6sSE/image.png","https://cdn.steemitimages.com/DQmUcrMY4iNzDeT13w33tre3kad6yNGrkBejcEJRahBGDdv/image.png"]}
created2020-08-31 11:43:30
last_update2020-08-31 11:43:30
depth0
children4
last_payout2020-09-07 11:43:30
cashout_time1969-12-31 23:59:59
total_payout_value4.292 HBD
curator_payout_value4.005 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length5,135
author_reputation118,592,211,436,406
root_title"[Python #15] [Django #8] 开发完,需测试"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,371,011
net_rshares27,829,163,627,492
author_curate_reward""
vote details (76)
@annepink ·
@tipu curate
properties (22)
authorannepink
permlinkqfye3b
categoryhive-105017
json_metadata{"users":["tipu"],"app":"hiveblog/0.1"}
created2020-09-01 00:16:24
last_update2020-09-01 00:16:24
depth1
children1
last_payout2020-09-08 00:16:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length12
author_reputation1,030,887,736,784,770
root_title"[Python #15] [Django #8] 开发完,需测试"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,382,178
net_rshares0
@tipu ·
<a href="https://tipu.online/hive_curator?annepink" target="_blank">Upvoted  &#128076;</a> (Mana: 10/45)
properties (22)
authortipu
permlinkre-qfye3b-20200901t001630
categoryhive-105017
json_metadata""
created2020-09-01 00:16:33
last_update2020-09-01 00:16:33
depth2
children0
last_payout2020-09-08 00:16:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length104
author_reputation55,916,482,498,885
root_title"[Python #15] [Django #8] 开发完,需测试"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,382,182
net_rshares0
@annepink ·
早啊俊🌻
properties (22)
authorannepink
permlinkqfye4j
categoryhive-105017
json_metadata{"app":"hiveblog/0.1"}
created2020-09-01 00:17:09
last_update2020-09-01 00:17:09
depth1
children1
last_payout2020-09-08 00:17:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4
author_reputation1,030,887,736,784,770
root_title"[Python #15] [Django #8] 开发完,需测试"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,382,189
net_rshares0
@june0620 ·
早啊萍萍~🙃
🙏tipu
properties (22)
authorjune0620
permlinkre-annepink-qfyejg
categoryhive-105017
json_metadata{"tags":["hive-105017"],"app":"peakd/2020.08.3"}
created2020-09-01 00:26:06
last_update2020-09-01 00:26:06
depth2
children0
last_payout2020-09-08 00:26:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length12
author_reputation118,592,211,436,406
root_title"[Python #15] [Django #8] 开发完,需测试"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,382,290
net_rshares0