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个可以接受,正好~ 哈哈 👇  ##### 编写测试代码 先简单学一下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了。  每当功能上有改动时可以用这个用例测试了。 *** **[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/
author | june0620 |
---|---|
permlink | python-15-django-8 |
category | hive-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"]} |
created | 2020-08-31 11:43:30 |
last_update | 2020-08-31 11:43:30 |
depth | 0 |
children | 4 |
last_payout | 2020-09-07 11:43:30 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 4.292 HBD |
curator_payout_value | 4.005 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 5,135 |
author_reputation | 118,592,211,436,406 |
root_title | "[Python #15] [Django #8] 开发完,需测试" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 99,371,011 |
net_rshares | 27,829,163,627,492 |
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
livingfree | 0 | 315,578,792,503 | 4% | ||
magicmonk | 0 | 2,523,678,930,037 | 100% | ||
always1success | 0 | 444,690,814,250 | 100% | ||
sweetsssj | 0 | 13,880,626,764,810 | 33% | ||
tumutanzi | 0 | 553,105,151 | 50% | ||
created | 0 | 659,603,756,897 | 4% | ||
coldhair | 0 | 892,445,338 | 50% | ||
centering | 0 | 1,900,158,564,028 | 100% | ||
shenchensucc | 0 | 18,087,900,265 | 30% | ||
tipu | 0 | 3,449,991,046,042 | 9% | ||
kimzwarch | 0 | 8,798,283,636 | 4% | ||
june0620 | 0 | 498,863,259,138 | 100% | ||
mawit07 | 0 | 2,535,107,583 | 50% | ||
xiaoshancun | 0 | 12,502,522,059 | 100% | ||
minloulou | 0 | 2,481,589,365 | 10% | ||
lindalex | 0 | 580,349,302 | 50% | ||
cnbuddy | 0 | 937,459,857,838 | 100% | ||
itchyfeetdonica | 0 | 52,065,242,886 | 50% | ||
nokodemion | 0 | 10,121,044,522 | 100% | ||
dudream | 0 | 57,945,901,145 | 100% | ||
bartheek | 0 | 12,823,079,484 | 4.5% | ||
suhunter | 0 | 961,902,227 | 50% | ||
calist | 0 | 4,200,011,982 | 100% | ||
udabeu | 0 | 9,926,496,849 | 30% | ||
jsj1215 | 0 | 3,692,281,632 | 100% | ||
yasu | 0 | 6,630,809,051 | 100% | ||
happy-soul | 0 | 26,206,866,338 | 4.5% | ||
frassman | 0 | 4,155,140,835 | 25% | ||
donekim | 0 | 2,680,332,526 | 100% | ||
lucky2015 | 0 | 8,064,844,838 | 100% | ||
gghite | 0 | 183,830,145,722 | 100% | ||
quochuy | 0 | 121,099,545,946 | 8.13% | ||
julialee66 | 0 | 1,098,975,599,504 | 8.5% | ||
andrewma | 0 | 11,642,258,327 | 50% | ||
meins0815 | 0 | 11,071,486,210 | 23% | ||
crimo | 0 | 606,970,091 | 11.5% | ||
longer | 0 | 1,067,856,539 | 2.25% | ||
coder-bts | 0 | 4,578,453,147 | 50% | ||
donald.porter | 0 | 7,801,522,828 | 18% | ||
daath | 0 | 715,006,828 | 100% | ||
melaniewang | 0 | 8,349,787,502 | 50% | ||
changxiu | 0 | 5,053,333,843 | 50% | ||
bluengel | 0 | 16,655,169,186 | 100% | ||
laissez-faire | 0 | 68,612,878 | 100% | ||
cherryzz | 0 | 162,903,765,755 | 50% | ||
forecasteem | 0 | 79,414,387,375 | 100% | ||
moneytron | 0 | 6,497,438,906 | 100% | ||
starrouge | 0 | 1,047,897,507 | 50% | ||
wherein | 0 | 445,473,500,411 | 100% | ||
steemfriends | 0 | 12,464,107,434 | 100% | ||
zerofive | 0 | 955,763,949 | 50% | ||
jacuzzi | 0 | 1,440,204,627 | 4.5% | ||
tagalong | 0 | 1,530,859,458 | 100% | ||
cnstm | 0 | 275,239,036,396 | 100% | ||
meins0816 | 0 | 2,700,421,456 | 75% | ||
lianjingmedia | 0 | 991,013,553 | 100% | ||
cpt-sparrow | 0 | 4,074,197,448 | 100% | ||
kryptogames | 0 | 42,589,242,611 | 9% | ||
tina3721 | 0 | 4,865,573,237 | 50% | ||
andyhsia | 0 | 7,813,031,255 | 100% | ||
minigame | 0 | 329,841,102,941 | 100% | ||
solomon.grundy | 0 | 3,039,109,429 | 75% | ||
cryptogambit | 0 | 1,440,875,335 | 7.5% | ||
tokenindustry | 0 | 2,930,357,685 | 80% | ||
bcm | 0 | 3,437,665,584 | 6.75% | ||
mein2070 | 0 | 2,707,159,432 | 75% | ||
dappstats | 0 | 3,637,635,186 | 15% | ||
cnbuddy-reward | 0 | 75,296,925,887 | 100% | ||
real3earch | 0 | 9,365,603,542 | 100% | ||
gmlrecordz | 0 | 2,461,121,305 | 25% | ||
toni.pal | 0 | 0 | 0.52% | ||
kgsupport | 0 | 1,747,690,098 | 50% | ||
hiveyoda | 0 | 10,454,492,501 | 4% | ||
logicforce | 0 | 2,034,089,947 | 50% | ||
hivve | 0 | 1,240,821,279 | 100% | ||
livinglava | 0 | 1,455,744,855 | 100% |
@tipu curate
author | annepink |
---|---|
permlink | qfye3b |
category | hive-105017 |
json_metadata | {"users":["tipu"],"app":"hiveblog/0.1"} |
created | 2020-09-01 00:16:24 |
last_update | 2020-09-01 00:16:24 |
depth | 1 |
children | 1 |
last_payout | 2020-09-08 00:16:24 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 12 |
author_reputation | 1,030,887,736,784,770 |
root_title | "[Python #15] [Django #8] 开发完,需测试" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 99,382,178 |
net_rshares | 0 |
<a href="https://tipu.online/hive_curator?annepink" target="_blank">Upvoted 👌</a> (Mana: 10/45)
author | tipu |
---|---|
permlink | re-qfye3b-20200901t001630 |
category | hive-105017 |
json_metadata | "" |
created | 2020-09-01 00:16:33 |
last_update | 2020-09-01 00:16:33 |
depth | 2 |
children | 0 |
last_payout | 2020-09-08 00:16:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 104 |
author_reputation | 55,916,482,498,885 |
root_title | "[Python #15] [Django #8] 开发完,需测试" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 99,382,182 |
net_rshares | 0 |
早啊俊🌻
author | annepink |
---|---|
permlink | qfye4j |
category | hive-105017 |
json_metadata | {"app":"hiveblog/0.1"} |
created | 2020-09-01 00:17:09 |
last_update | 2020-09-01 00:17:09 |
depth | 1 |
children | 1 |
last_payout | 2020-09-08 00:17:09 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 4 |
author_reputation | 1,030,887,736,784,770 |
root_title | "[Python #15] [Django #8] 开发完,需测试" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 99,382,189 |
net_rshares | 0 |
早啊萍萍~🙃 🙏tipu
author | june0620 |
---|---|
permlink | re-annepink-qfyejg |
category | hive-105017 |
json_metadata | {"tags":["hive-105017"],"app":"peakd/2020.08.3"} |
created | 2020-09-01 00:26:06 |
last_update | 2020-09-01 00:26:06 |
depth | 2 |
children | 0 |
last_payout | 2020-09-08 00:26:06 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 12 |
author_reputation | 118,592,211,436,406 |
root_title | "[Python #15] [Django #8] 开发完,需测试" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 99,382,290 |
net_rshares | 0 |