# 7.常见套路




在使用pytest的时候，下面这些问题我们可能会经常遇到，这里给出官方的解决方案，按照套路来执行就可以了。

### 基础用法

* [把命令行参数传入到用例](https://docs.pytest.org/en/latest/example/simple.html#pass-different-values-to-a-test-function-depending-on-command-line-options)
* [动态添加命令行参数](https://docs.pytest.org/en/latest/example/simple.html#dynamically-adding-command-line-options)
* [根据命令行参数来忽略用例执行](https://docs.pytest.org/en/latest/example/simple.html#control-skipping-of-tests-according-to-command-line-option)
* [编写集成度更好的辅助断言](https://docs.pytest.org/en/latest/example/simple.html#writing-well-integrated-assertion-helpers)
* [判断是否由pytest执行](https://docs.pytest.org/en/latest/example/simple.html#detect-if-running-from-within-a-pytest-run)
* [在测试报告的头部添加内容](https://docs.pytest.org/en/latest/example/simple.html#adding-info-to-test-report-header)
* [统计用例运行时间](https://docs.pytest.org/en/latest/example/simple.html#profiling-test-duration)
* [定义测试步骤，也就是让用例按照一定的顺序执行](https://docs.pytest.org/en/latest/example/simple.html#profiling-test-duration)
* [Package/Directory-level fixtures (setups)](https://docs.pytest.org/en/latest/example/simple.html#package-directory-level-fixtures-setups)
* [在报告和用例失败之前添加钩子](https://docs.pytest.org/en/latest/example/simple.html#post-process-test-reports-failures)
* [在fixtures中访问测试结果](https://docs.pytest.org/en/latest/example/simple.html#making-test-result-information-available-in-fixtures)
* [PYTEST_CURRENT_TEST环境变量](https://docs.pytest.org/en/latest/example/simple.html#pytest-current-test-environment-variable)
* [冻结pytest](https://docs.pytest.org/en/latest/example/simple.html#freezing-pytest)

### 参数化

* [根据命令行参数来组合测试参数](https://docs.pytest.org/en/latest/example/parametrize.html#generating-parameters-combinations-depending-on-command-line)
* [配置test ID](https://docs.pytest.org/en/latest/example/parametrize.html#generating-parameters-combinations-depending-on-command-line)
* [快速创建测试场景的功能](https://docs.pytest.org/en/latest/example/parametrize.html#a-quick-port-of-testscenarios)
* [延迟参数资源加载](https://docs.pytest.org/en/latest/example/parametrize.html#deferring-the-setup-of-parametrized-resources)
* [间接参数](https://docs.pytest.org/en/latest/example/parametrize.html#apply-indirect-on-particular-arguments)
* [为不同的方法设置不同的参数](https://docs.pytest.org/en/latest/example/parametrize.html#parametrizing-test-methods-through-per-class-configuration)
* [在多个fixture中使用间接参数](https://docs.pytest.org/en/latest/example/parametrize.html#indirect-parametrization-with-multiple-fixtures)
* [Indirect parametrization of optional implementations/imports](https://docs.pytest.org/en/latest/example/parametrize.html#indirect-parametrization-of-optional-implementations-imports)
* [单独的为每个参数化用例设置标记和ID](https://docs.pytest.org/en/latest/example/parametrize.html#set-marks-or-test-id-for-individual-parametrized-test)




原始封面

![课程图片](https://images.unsplash.com/photo-1444090542259-0af8fa96557e?w=300)


