python单元测试
2021/8/25 1:36:01
本文主要是介绍python单元测试,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
python3直接使用unittest标准库
1. 需要继承unittest.TestCase类
2. 测试方法需要取名为test_xxx()
import unittest from unittest import mock from unittest.mock import patch import UnitTestDemo class Caculator: def add(self, n1, n2): return n1 + n2 def add_and_multiply(n1, n2): sum_res = n1 + n2 multi_res = multiply_func(n1, n2) return sum_res, multi_res def multiply_func(n1, n2): return n1 * n2 class TestAdd(unittest.TestCase): def test_add(self): caculator = Caculator() caculator.add = mock.Mock(return_value=100) res = caculator.add(1, 10) print(f"sum:{res}") self.assertEqual(res, 100) @patch("mock_demo.multiply_func") def test_add_and_multiply(self, _mock_multiply): _mock_multiply.return_value = 1000 sum_res, multi_res = add_and_multiply(10, 20) print(f"sum:{sum_res}, multi_res:{multi_res}") if __name__ == '__main__': print(UnitTestDemo.mock_demo.multiply_func(10, 11))
这篇关于python单元测试的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南