Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3.8中unittest的奇怪之处-不检测测试_Python_Python 3.x_Python Unittest - Fatal编程技术网

Python3.8中unittest的奇怪之处-不检测测试

Python3.8中unittest的奇怪之处-不检测测试,python,python-3.x,python-unittest,Python,Python 3.x,Python Unittest,这是一个非常奇怪的问题,但是Python中的unittest实际上并没有在测试用例中进行测试 这是我正在使用的代码,包括我正在测试的函数和我试图在tests.py脚本中使用的unittest代码: import unittest import bs4 # This is the function we're testing def zerolength_link_test(post_contents: str) -> bool: # Returns 'True' if ther

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

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

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()
导入单元测试
进口bs4
#这是我们正在测试的功能
def zerolength链接测试(post内容:str)->bool:
#如果此处存在长度为零的链接,则返回“True”。
bs=bs4.BeautifulSoup(post_内容'html.parser')
对于bs.find_all('a')中的链接:
如果':

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

导入单元测试
进口bs4
#这是我们正在测试的功能
def zerolength链接测试(post内容:str)->bool:
#如果此处存在长度为零的链接,则返回“True”。
bs=bs4.BeautifulSoup(post_内容'html.parser')
对于bs.find_all('a')中的链接:

如果“我认为你的每个测试方法都应该被命名为:
test.*
”,那么我现在觉得自己很愚蠢。谢谢。没问题,生活中每个程序员都会遇到pal@ThomasWard
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()