***************************** 代码库 ***************************** 单元测试 ====================================== Xunit风格 .. tabs:: .. tab:: pytest .. tabs:: .. tab:: 测试代码 .. literalinclude:: /../../testCode/unittest/test_xunit_style.py .. tab:: conftest.py .. literalinclude:: /../../testCode/unittest/conftest.py .. tab:: testify .. literalinclude:: /../../testCode/unittest/xunit_style_test.go :language: golang 单脚本场景下的单元测试 ------------------------------- 场景: 一个main脚本放在服务器用定时任务去跑 .. code-block:: python def main(): pass if __name__ == '__main__': main() 创建一个main_test.py编写测试用例 接口测试 ========================================== .. tabs:: .. tab:: drf .. literalinclude:: /../../reference/testing/apitest/drf.py DDT风格 ========================================= .. tabs:: .. tab:: pytest .. literalinclude:: /../../testCode/parametrize/pytest_.py .. tab:: testing 在testing包层面操作,不在testify .. literalinclude:: /../../testCode/parametrize/parametrize_test.go .. tab:: unittest .. literalinclude:: /../../testCode/parametrize/unittest_.py 基于容器的外部依赖 =========================================== https://gitee.com/luzhenxiong/bilibili-tests/tree/master/testcontainers_ * python: https://testcontainers-python.readthedocs.io/en/latest/index.html .. tabs:: .. tab:: pytest .. literalinclude:: /../../reference/testing/testcontainer/test_py.py .. tab:: testify https://testcontainers.com/guides/getting-started-with-testcontainers-for-go/#_reusing_the_containers_and_running_multiple_tests_as_a_suite .. tabs:: .. tab:: repo_suite_test.go .. literalinclude:: /../../reference/testing/testcontainer/testcontainers-go-demo/customer/repo_suite_test.go :language: golang .. tab:: containers.go .. literalinclude:: /../../reference/testing/testcontainer/testcontainers-go-demo/testhelpers/containers.go :language: golang mock技术 ============================================== .. tabs:: .. tab:: pytest .. literalinclude:: /../../testCode/mock/pytest_.py .. tab:: gomonkey https://github.com/agiledragon/gomonkey :file:`app/internal/logic/sendmsglogic_test.go` .. literalinclude:: mock/sendmsglogic_test.go :language: golang 命令行执行测试 =============================================== .. tabs:: .. tab:: pytest .. code-block:: console pytest -v .. tab:: testing .. code-block:: console go test ./... -v 测试报告 ===================================================== .. tabs:: .. tab:: go https://github.com/gotestyourself/gotestsum .. code-block:: console go run gotest.tools/gotestsum@latest 测试异步函数 ===================================================== `pytest-asyncio `_ .. code-block:: python @pytest.mark.asyncio async def test_some_asyncio_code(): res = await library.do_something() assert b'expected result' == res 测试覆盖 ===================================================== .. tabs:: .. group-tab:: coverage .. code-block:: console # 运行 $ coverage run --source=app -m pytest # 命令行输出报告 $ coverage report --show-missing # 测试结果保存到html $ coverage html --title "${@-coverage}" .. seealso:: fastapi模板项目的 `test.sh `_