我在Salesforce,Jira等系统之间进行了大量的集成工作......我对unit-testing
我的代码很满意,因为它是一个简单的,自包含的代码块。我在为涉及许多系统的应用程序编写全面测试时遇到了更多困难......
以下是我正在重新考虑从内部公司网站上删除代码覆盖率数据的工作的示例:
def scrape_projects(
driver: webdriver,
project_names: list,
col_cnt: int,
project_tup: namedtuple) -> (list, dict):
"""
Take a list of project names (must be the names as they appear on the
page itself), follows the link to teh project and then scrapes the data.
Once the data has been scraped it is dumped into 'namedtuples' for easy
conversion to dictionaries.
:param driver: A selenium.webdriver object.
:param project_names: A list of project names.
:param col_cnt: The number of expected data columns on the page for a
specific project type. For example, Ruby projects have 7 columns of data.
:param project_tup: A namedtuple suitable for the expected columns of a
project type.
:return: A list of namedtuples containing the project data.
"""
alerts = {}
project_tups = []
for name in project_names:
logger.info('On project {name}'.format(name=name))
link = driver.find_element_by_link_text(name)
link.click()
data = [item.text.replace('%', '') for item in
driver.find_elements_by_tag_name('td') if item.text]
logger.debug('Page data found: {!r}'.format(data))
# create offsets to split up the data
offsets = tuple(range(0, len(data), col_cnt))
data_len = len(data)
logger.debug(f'Aggregate total for data columns: {data_len}')
try:
if data_len % col_cnt != 0:
raise TypeError(
'The column count is not compatible with the given '
'project type.')
for offset in offsets:
tup = project_tup(name, *data[offset:offset + col_cnt])
logger.debug('Project tuple: {!r}'.format(project_tup))
project_tups.append(tup)
except TypeError as e:
logger.info(
f'Error occurred during processing of {name}.',
exc_info=True
)
alerts[name] = e
finally:
driver.back()
return project_tups, alerts
我是否只是编写一个测试来定位一个已知的项目,并确保它返回预期的输出?由于某些原因,这对我来说似乎很脆弱。无论如何,我只是想提高我对适当测试方法的认识。