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

pixabay

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

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

组合用例

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

Namevalue1value2value3value4
TAGS搜索不到的值空listlist 大小为1的正常值空字符串
TITLES搜索不到的值空listlist 大小为2的正常值特殊字符与数字
TEXTS搜索不到的值空listlist 大小为3的正常值空字符串

用 PICT 来组合上述用例应该会有不错的结果。
예전 글 [IT] MS PICT 的 Pairwise 测试 👇

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/

Sort:  

早啊俊🌻

早啊萍萍~🙃
🙏tipu