【问题标题】:Oddness with unittest in Python 3.8 - not detecting tests [duplicate]Python 3.8 中的 unittest 异常 - 未检测到测试 [重复]
【发布时间】:2020-09-16 19:42:51
【问题描述】:

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

这是我正在使用的代码,我正在测试的函数和我尝试在 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 驱动测试时做错了什么,或者这只是这个系统上的古怪之处吗?

【问题讨论】:

  • 我认为你的每个测试方法都应该命名为:test_* 。

标签: python python-3.x python-unittest


【解决方案1】:

Tests should start with test prefix:

三个单独的测试是用名称以字母 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()

【讨论】:

  • Bluh 我现在觉得自己很愚蠢。谢谢。
  • 没有问题的朋友@ThomasWard 在每个程序员的一生中都会遇到
猜你喜欢
  • 1970-01-01
  • 2020-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 2017-09-03
  • 1970-01-01
  • 2020-06-01
相关资源
最近更新 更多