Kob*_*i K 7 python unit-testing pytest
我有一个测试类来测试我的方法但是我有一些传递self的问题,它们都在class和test类中.
我的方法:
def get_all_links(self):
"""return all the links inside an html
:return: list of links from an html page
:rtype: list
"""
return self.html.find_all('a')
Run Code Online (Sandbox Code Playgroud)
我的测试用例:
@parameterized.expand(["http://www.google.com", "http://www.walla.com"])
def test_get_all_links_known_links(self, known_link):
"""check get_all_links with a known link list
:param known_link: lick to find
:type known_link: str
"""
html = Parser(open(os.path.normpath(os.path.join(self.root, "test.html"))))
self.assertTrue(any([known_link in str(l) for l in html.get_all_links()]))
Run Code Online (Sandbox Code Playgroud)
错误:
E TypeError: test_get_all_links_known_links() takes exactly 2 arguments (1 given)
/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py:329: TypeError
...
Run Code Online (Sandbox Code Playgroud)
你真的不需要unittest.TestCase
在这里子类化:
您还可以使用pytest “参数化”测试:
例子:
import pytest
from app.objects import Root # Example
known_links = [
"http://www.google.com",
"http://www.walla.com"
]
@pytest.fixture()
def root(request):
return Root() # Root object
@pytest.mark.parametrize("known_links", known_links)
def test_get_all_links_known_links(root, known_link):
html = Parser(
open(os.path.normpath(os.path.join(root, "test.html")))
)
assert any([known_link in str(l) for l in html.get_all_links()])
Run Code Online (Sandbox Code Playgroud)
看:
归档时间: |
|
查看次数: |
1457 次 |
最近记录: |