首页 > 解决方案 > Python 3.8 中 unittest 的奇怪之处 - 未检测到测试

问题描述

这是一个非常奇怪的问题,但unittest在 Python 中实际上并没有在测试用例中提取测试。

这是我正在使用的代码,我正在测试的函数和我试图在tests.py脚本中使用的单元测试代码:

import unittest
import bs4


# This is the function we're testing
def zerolength_link_test(post_contents: str) -> bool:
    # Returns 'True' if there is a zero length link in here.
    bs = bs4.BeautifulSoup(post_contents, 'html.parser')
    for link in bs.find_all('a'):
        if '<img ' in str(link):
            # Image embeds in links are not zero-length for this case.
            continue
        if len(link.text) == 0:
            return True
        if link.text.isspace() or not link.text.isprintable():
            return True

    return False


class ZeroLengthLinkTests(unittest.TestCase):

    def whitespace_only_link(self):
        test = 'This is a test at <a href="https://google.com"> </a> whitespace-only ' \
               'links which are effectively zero-length.'
        self.assertTrue(zerolength_link_test(test))

    def zero_length_link_nonobfuscated(self):
        test = "This is a test of <a href='google.com'></a> actual zero-length link text."
        self.assertTrue(zerolength_link_test(test))

    def zero_length_link_tag_obfuscation(self):
        test = "This is a test of <a href='google.com'><em></em></a> z" \
               "ero length links obfuscated by tags."
        self.assertTrue(zerolength_link_test(test))

    def unprintable_only_link(self):
        test = "This one has unprintable characters <a href='google.com'>\t\f\r\n</a> in the link."
        self.assertTrue(zerolength_link_test(test))

    def not_zero_length_link(self):
        test = "This is a test of <a href='https://google.com'>an actual link to " \
               "Google</a> that is not Zero Length."
        self.assertFalse(zerolength_link_test(test))

    def whitespace_only_link_tag_obfuscation(self):
        test = "This is a test of a whitespace only link <a href='google.com'><span> </span></a>" \
               " obfuscated with span tags."
        self.assertTrue(zerolength_link_test(test))


if __name__ == "__main__":
    unittest.main()

不幸的是,当我运行它时,系统显示“0 个测试运行”,这表明它无法找到测试用例,反过来又是 fubar。

这是 Ubuntu 20.04 (3.8.2) 上的系统 Python 3,但也使用 3.8.5 的本地安装(通过用户空间中的 pyenv 安装)进行了测试。

我过去编写的其他测试套件在这方面运行良好,并且以类似的方式设置。

我在编写驱动测试时做错了什么unittest,或者这只是这个系统上的古怪之处,你会说吗?

标签: pythonpython-3.xpython-unittest

解决方案


测试应以test前缀开头:

这三个单独的测试是用名称以字母 test 开头的方法定义的。此命名约定告知测试运行程序哪些方法代表测试。

import unittest
import bs4


# This is the function we're testing
def zerolength_link_test(post_contents: str) -> bool:
    # Returns 'True' if there is a zero length link in here.
    bs = bs4.BeautifulSoup(post_contents, 'html.parser')
    for link in bs.find_all('a'):
        if '<img ' in str(link):
            # Image embeds in links are not zero-length for this case.
            continue
        if len(link.text) == 0:
            return True
        if link.text.isspace() or not link.text.isprintable():
            return True

    return False


class ZeroLengthLinkTests(unittest.TestCase):

    def test_whitespace_only_link(self):
        test = 'This is a test at <a href="https://google.com"> </a> whitespace-only ' \
               'links which are effectively zero-length.'
        self.assertTrue(zerolength_link_test(test))

    def test_zero_length_link_nonobfuscated(self):
        test = "This is a test of <a href='google.com'></a> actual zero-length link text."
        self.assertTrue(zerolength_link_test(test))

    def test_zero_length_link_tag_obfuscation(self):
        test = "This is a test of <a href='google.com'><em></em></a> z" \
               "ero length links obfuscated by tags."
        self.assertTrue(zerolength_link_test(test))

    def test_unprintable_only_link(self):
        test = "This one has unprintable characters <a href='google.com'>\t\f\r\n</a> in the link."
        self.assertTrue(zerolength_link_test(test))

    def test_not_zero_length_link(self):
        test = "This is a test of <a href='https://google.com'>an actual link to " \
               "Google</a> that is not Zero Length."
        self.assertFalse(zerolength_link_test(test))

    def test_whitespace_only_link_tag_obfuscation(self):
        test = "This is a test of a whitespace only link <a href='google.com'><span> </span></a>" \
               " obfuscated with span tags."
        self.assertTrue(zerolength_link_test(test))


if __name__ == "__main__":
    unittest.main()

推荐阅读