Pytest单元测试系列[v1.0.0][pytest-html测试报告]

安装pytest-html

在命令行执行命令pip install pytest-html


D:\PythonPrograms\Python_Pytest\TestScripts>pip install pytest-html
Collecting pytest-html
  Downloading https://files.pythonhosted.org/packages/f7/05/7cc56180a9c4b00b4055ffdad3763884000082a471c2e7251e97ea055932/pytest_html-2.0.0-py2.py3-none-any.whl
Collecting pytest-metadata (from pytest-html)
  Downloading https://files.pythonhosted.org/packages/ce/8f/d0542e1aa0e23d902ce6acce2790736473da94453a36bdc7829f25734199/pytest_metadata-1.8.0-py2.py3-none-any.whl
Collecting pytest>=5.0 (from pytest-html)
  Downloading https://files.pythonhosted.org/packages/ca/e1/2f229554e5c273962fae8b286395d5bbcc7bef276d2b40e1bad954993db2/pytest-5.1.3-py3-none-any.whl (224kB)
    100% |████████████████████████████████| 225kB 769kB/s
Requirement already satisfied: py>=1.5.0 in c:\python37\lib\site-packages (from pytest>=5.0->pytest-html) (1.8.0)
Requirement already satisfied: importlib-metadata>=0.12; python_version < "3.8" in c:\python37\lib\site-packages (from pytest>=5.0->pytest-html) (0.17)
Requirement already satisfied: packaging in c:\python37\lib\site-packages (from pytest>=5.0->pytest-html) (19.0)
Requirement already satisfied: atomicwrites>=1.0 in c:\python37\lib\site-packages (from pytest>=5.0->pytest-html) (1.3.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest>=5.0->pytest-html) (0.4.1)
Requirement already satisfied: more-itertools>=4.0.0 in c:\python37\lib\site-packages (from pytest>=5.0->pytest-html) (7.0.0)
Requirement already satisfied: wcwidth in c:\python37\lib\site-packages (from pytest>=5.0->pytest-html) (0.1.7)
Requirement already satisfied: pluggy<1.0,>=0.12 in c:\python37\lib\site-packages (from pytest>=5.0->pytest-html) (0.12.0)
Requirement already satisfied: attrs>=17.4.0 in c:\python37\lib\site-packages (from pytest>=5.0->pytest-html) (19.1.0)
Requirement already satisfied: zipp>=0.5 in c:\python37\lib\site-packages (from importlib-metadata>=0.12; python_version < "3.8"->pytest>=5.0->pytest-html) (0.5.1)
Requirement already satisfied: pyparsing>=2.0.2 in c:\python37\lib\site-packages (from packaging->pytest>=5.0->pytest-html) (2.4.0)
Requirement already satisfied: six in c:\python37\lib\site-packages (from packaging->pytest>=5.0->pytest-html) (1.12.0)
Installing collected packages: pytest, pytest-metadata, pytest-html
  Found existing installation: pytest 4.0.2
    Uninstalling pytest-4.0.2:
      Successfully uninstalled pytest-4.0.2
Successfully installed pytest-5.1.3 pytest-html-2.0.0 pytest-metadata-1.8.0

使用–html参数执行用例

执行命令pytest --html=[report path]
生成报告样式如下:
Pytest单元测试系列[v1.0.0][pytest-html测试报告]_第1张图片

执行异常

如果环境里有allure,执行用例报如下错误

D:\PythonPrograms\Python_Pytest\TestScripts>pytest test_one.py test_two.py test_three.py test_four.py test_five.py --html=D:/report.html
=========================================================================== test session starts ===========================================================================
platform win32 -- Python 3.7.2, pytest-5.1.3, py-1.8.0, pluggy-0.12.0
rootdir: D:\PythonPrograms\Python_Pytest, inifile: pytest.ini
plugins: allure-adaptor-1.7.10, cov-2.7.1, html-2.0.0, metadata-1.8.0
collected 12 items
INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "c:\python37\lib\site-packages\_pytest\main.py", line 191, in wrap_session
INTERNALERROR>     session.exitstatus = doit(config, session) or 0
INTERNALERROR>   File "c:\python37\lib\site-packages\_pytest\main.py", line 234, in _main
INTERNALERROR>     config.hook.pytest_collection(session=session)
INTERNALERROR>   File "c:\python37\lib\site-packages\pluggy\hooks.py", line 289, in __call__
INTERNALERROR>     return self._hookexec(self, self.get_hookimpls(), kwargs)
INTERNALERROR>   File "c:\python37\lib\site-packages\pluggy\manager.py", line 87, in _hookexec
INTERNALERROR>     return self._inner_hookexec(hook, methods, kwargs)
INTERNALERROR>   File "c:\python37\lib\site-packages\pluggy\manager.py", line 81, in <lambda>
INTERNALERROR>     firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
INTERNALERROR>   File "c:\python37\lib\site-packages\pluggy\callers.py", line 208, in _multicall
INTERNALERROR>     return outcome.get_result()
INTERNALERROR>   File "c:\python37\lib\site-packages\pluggy\callers.py", line 80, in get_result
INTERNALERROR>     raise ex[1].with_traceback(ex[2])
INTERNALERROR>   File "c:\python37\lib\site-packages\pluggy\callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "c:\python37\lib\site-packages\_pytest\main.py", line 244, in pytest_collection
INTERNALERROR>     return session.perform_collect()
INTERNALERROR>   File "c:\python37\lib\site-packages\_pytest\main.py", line 438, in perform_collect
INTERNALERROR>     self.config.pluginmanager.check_pending()
INTERNALERROR>   File "c:\python37\lib\site-packages\pluggy\manager.py", line 270, in check_pending
INTERNALERROR>     % (name, hookimpl.plugin),
INTERNALERROR> pluggy.manager.PluginValidationError: unknown hook 'pytest_namespace' in plugin <module 'allure.pytest_plugin' from 'c:\\python37\\lib\\site-packages\\allure
\\pytest_plugin.py'>

=========================================================================== 3 warnings in 0.06s ===========================================================================

原因是pytest版本和allure不匹配,卸载了pytest,重新安装pytest=4.0.2版本
然后发现pytest-html=2.0.0版本又与pytest=4.0.2版本不兼容,得到如下提示:

D:\PythonPrograms\Python_Pytest\TestScripts>pip install pytest==4.0.2
Collecting pytest==4.0.2
  Using cached https://files.pythonhosted.org/packages/19/80/1ac71d332302a89e8637456062186bf397abc5a5b663c1919b73f4d68b1b/pytest-4.0.2-py2.py3-none-any.whl
Requirement already satisfied: py>=1.5.0 in c:\python37\lib\site-packages (from pytest==4.0.2) (1.8.0)
Requirement already satisfied: attrs>=17.4.0 in c:\python37\lib\site-packages (from pytest==4.0.2) (19.1.0)
Requirement already satisfied: six>=1.10.0 in c:\python37\lib\site-packages (from pytest==4.0.2) (1.12.0)
Requirement already satisfied: more-itertools>=4.0.0 in c:\python37\lib\site-packages (from pytest==4.0.2) (7.0.0)
Requirement already satisfied: atomicwrites>=1.0 in c:\python37\lib\site-packages (from pytest==4.0.2) (1.3.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest==4.0.2) (0.4.1)
Requirement already satisfied: setuptools in c:\python37\lib\site-packages (from pytest==4.0.2) (40.6.2)
Requirement already satisfied: pluggy>=0.7 in c:\python37\lib\site-packages (from pytest==4.0.2) (0.12.0)
Requirement already satisfied: importlib-metadata>=0.12 in c:\python37\lib\site-packages (from pluggy>=0.7->pytest==4.0.2) (0.17)
Requirement already satisfied: zipp>=0.5 in c:\python37\lib\site-packages (from importlib-metadata>=0.12->pluggy>=0.7->pytest==4.0.2) (0.5.1)
pytest-html 2.0.0 has requirement pytest>=5.0, but you'll have pytest 4.0.2 which is incompatible.
Installing collected packages: pytest
Successfully installed pytest-4.0.2

然而,实际执行用例的时候并未影响生成html的测试报告

你可能感兴趣的:(Pytest单元测试系列[v1.0.0][pytest-html测试报告])