test: use pytest parametrize mark
This commit is contained in:
parent
d7a065c31e
commit
f2e7d754d6
|
|
@ -1,8 +1,10 @@
|
||||||
"""Test module `fetch_holidays`. """
|
"""Test module `fetch_holidays`. """
|
||||||
import json
|
import json
|
||||||
import sys
|
|
||||||
|
|
||||||
from fetch_holidays import CustomJSONEncoder, DescriptionParser, get_paper_urls, get_rules, get_paper
|
import pytest
|
||||||
|
|
||||||
|
from fetch_holidays import (CustomJSONEncoder, DescriptionParser, get_paper,
|
||||||
|
get_paper_urls, get_rules)
|
||||||
|
|
||||||
from .filetools import _file_path
|
from .filetools import _file_path
|
||||||
|
|
||||||
|
|
@ -15,8 +17,11 @@ def test_get_paper_urls():
|
||||||
|
|
||||||
|
|
||||||
def test_get_rules():
|
def test_get_rules():
|
||||||
assert(list(get_rules(get_paper('http://www.gov.cn/zhengce/content/2019-03/22/content_5375877.htm')))
|
assert (
|
||||||
== [('劳动节', '2019年5月1日至4日放假调休,共4天。4月28日(星期日)、5月5日(星期日)上班。')])
|
list(get_rules(get_paper(
|
||||||
|
'http://www.gov.cn/zhengce/content/2019-03/22/content_5375877.htm')))
|
||||||
|
== [('劳动节',
|
||||||
|
'2019年5月1日至4日放假调休,共4天。4月28日(星期日)、5月5日(星期日)上班。')])
|
||||||
|
|
||||||
|
|
||||||
def _normalize(iterable):
|
def _normalize(iterable):
|
||||||
|
|
@ -24,20 +29,14 @@ def _normalize(iterable):
|
||||||
key=lambda x: x['date'])
|
key=lambda x: x['date'])
|
||||||
|
|
||||||
|
|
||||||
def _generate_tests():
|
def _description_parsing_cases():
|
||||||
with open(_file_path('description_parsing_cases.json'), 'r', encoding='utf-8', ) as f:
|
with open(_file_path('description_parsing_cases.json'), 'r', encoding='utf-8', ) as f:
|
||||||
cases = json.load(f)
|
return json.load(f)
|
||||||
|
|
||||||
def create_test(case):
|
|
||||||
def _test():
|
|
||||||
year, description, expected = case['year'], case['description'], case['expected']
|
|
||||||
assert _normalize(DescriptionParser(
|
|
||||||
description, year).parse()) == _normalize(expected), case
|
|
||||||
return _test
|
|
||||||
|
|
||||||
for index, case in enumerate(cases, 1):
|
|
||||||
setattr(sys.modules[__name__],
|
|
||||||
f'test_description_parser_{index}', create_test(case))
|
|
||||||
|
|
||||||
|
|
||||||
_generate_tests()
|
@pytest.mark.parametrize('case', _description_parsing_cases())
|
||||||
|
def test_parse_description(case):
|
||||||
|
|
||||||
|
year, description, expected = case['year'], case['description'], case['expected']
|
||||||
|
assert _normalize(DescriptionParser(
|
||||||
|
description, year).parse()) == _normalize(expected), case
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user