这是一个非常奇怪的问题,但是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个测试正在运行”,这表明它无法找到测试用例,从而造成了麻烦。
这是Ubuntu 20.04(3.8.2)上的系统Python 3,但还通过3.8.5本地安装(通过pyenv在用户空间中安装)进行了测试。
我过去编写的其他测试套件可以很好地运行,并且以类似的方式设置。
在编写unittest
驱动测试时我做错什么了吗,还是只在您说这个系统上有些古怪?
答案 0 :(得分: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()