关于农产品电子商务网站的建设,招商网站建站,电商资源网站,门户网站开发架构文章目录 01-单元测试基础什么是单元测试常用的文件结构运行单元测试 02. 断言函数03. Test Fixtures什么是Test Fixtures模块级别的Fixtures类级别的Fixtures方法级别的Fixtures 04.Mock python单元测试学习笔记1#xff1a;https://blog.csdn.net/qq_42761751/article/detai… 文章目录 01-单元测试基础什么是单元测试常用的文件结构运行单元测试 02. 断言函数03. Test Fixtures什么是Test Fixtures模块级别的Fixtures类级别的Fixtures方法级别的Fixtures 04.Mock python单元测试学习笔记1https://blog.csdn.net/qq_42761751/article/details/141144477?spm1001.2014.3001.5501
python单元测试学习笔记2 https://blog.csdn.net/qq_42761751/article/details/141202123?spm1001.2014.3001.5501
python单元测试学习笔记3 https://blog.csdn.net/qq_42761751/article/details/141233236?spm1001.2014.3001.5501
01-单元测试基础 什么是单元测试常用的文件结构编写第一个单元测试运行单元测试 什么是单元测试 单元测试是指一个自动化的测试 用来验证一小段代码单元的正确性例如只测试某个函数写的是否正确可以快速执行在独立的环境中执行 常用的文件结构 myprj是项目代码目录 tests是单元测试代码目录一般来说测试代码与项目代码文件目录结构一样在tests/basic中test_calculator.py必须以test_开头因为测试的工具会寻找所有以test开头的文件把他们当作单元测试文件运行,这算是一个规范 运行单元测试
myprj.basic中calculator.py代码
class Calculator:def add(self, *args):res 0for n in args:res nreturn restests.basic中test_calculator.py代码
import unittest
from myprj.basic.calculator import Calculator
文件要用test_开头测试类名要用Test开头class TestCalculator(unittest.TestCase):def test_add(self):测试类方法必须用test_开头通常分为三部分:setupactionassert# setupcal Calculator()excepted_result 10 # 期望输出为10# actionactual_result cal.add(2,3,5)# assert 判断执行结果与期望结果是否相等如果不相等则测试失败# 说明代码有问题self.assertEqual(excepted_result, actual_result)为了方便的运行测试
pip install nose
pip install coverage# 运行一个测试文件
python -m unittest -v tests.basic.test_calculator
# 运行所有测试文件
nosetests --with-doctest -v tests/basic/
# 统计测试覆盖率
nosetests --with-coverage --cover-erase -v tests/也可以使用pytest:
pytest --doctest-modules tests/02. 断言函数
常用断言函数
03. Test Fixtures 什么是Test Fixtures模块级别的Fixtures类级别的Fixtures方法级别的Fixtures 什么是Test Fixtures 在测试之前或者之后执行的函数或者方法被称之为Test Fixtures 比如在测试前需要先连接数据库在测试后需要断开与数据建库的连接 模块级别的Fixtures setUpModule() 在整个模块执行之前需要执行的函数 tearDownModule() 在整个模块执行结束之后需要执行的函数 import unittest
from myprj.fixtures.bank_account import BankAccountdef setUpModule():print(calling setUpModule)def tearDownModule():print(calling tearDownModule)class TestBankAccount(unittest.TestCase):def test_deposit_success(self):bank_account BankAccount(0)bank_account.deposit(10)self.assertEqual(10, bank_account.balance)def test_withdraw_success(self):bank_account BankAccount(10)bank_account.withdraw(10)self.assertEqual(0, bank_account.balance)上述代码中当单独执行test_deposit_success 与test_withdraw_success时候代码会自动执行setUpModule 与 tearDownModule. 当执行TestBankAccount进行测试时虽然有两个测试用例但是setUpModule 与 tearDownModule只会执行一次
类级别的Fixtures setUpClass() 在整类执行之前需要执行的函数 tearDownClass() 在整个类执行结束之后需要执行的函数 import unittest
from myprj.fixtures.bank_account import BankAccountdef setUpModule():print(calling setUpModule)def tearDownModule():print(calling tearDownModule)class TestBankAccount(unittest.TestCase):classmethoddef setUpClass(cls) - None:print(calling setUpClass)classmethoddef tearDownClass(cls) - None:print(calling tearDownClass)def test_deposit_success(self):bank_account BankAccount(0)bank_account.deposit(10)self.assertEqual(10, bank_account.balance)def test_withdraw_success(self):bank_account BankAccount(10)bank_account.withdraw(10)self.assertEqual(0, bank_account.balance)方法级别的Fixtures setUp() 在任何一个方法执行之前都会被执行 tearDown() import unittest
from myprj.fixtures.bank_account import BankAccountclass TestBankAccount(unittest.TestCase):def setUp(self) - None:每一个类方法都要新建一个对象在任何一个方法执行之前都会执行print(calling setUp)self.bank_account BankAccount(10)def tearDown(self) - None:print(calling tearDown)self.bank_account Nonedef test_deposit_success(self):self.bank_account.deposit(10)self.assertEqual(20, self.bank_account.balance)def test_withdraw_success(self):self.bank_account.withdraw(10)self.assertEqual(0, self.bank_account.balance)04.Mock 什么是MockMock和MagicMockMock实例 本文参考 https://www.bilibili.com/video/BV1SP4y1D7Wd/?spm_id_from333.999.0.0 文章转载自: http://www.morning.bkxnp.cn.gov.cn.bkxnp.cn http://www.morning.nhzps.cn.gov.cn.nhzps.cn http://www.morning.ryxdf.cn.gov.cn.ryxdf.cn http://www.morning.shinezoneserver.com.gov.cn.shinezoneserver.com http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.hjwkq.cn.gov.cn.hjwkq.cn http://www.morning.xylxm.cn.gov.cn.xylxm.cn http://www.morning.xqxrm.cn.gov.cn.xqxrm.cn http://www.morning.ldzss.cn.gov.cn.ldzss.cn http://www.morning.dwyyf.cn.gov.cn.dwyyf.cn http://www.morning.gjcdr.cn.gov.cn.gjcdr.cn http://www.morning.rlns.cn.gov.cn.rlns.cn http://www.morning.srjgz.cn.gov.cn.srjgz.cn http://www.morning.rydhq.cn.gov.cn.rydhq.cn http://www.morning.kwnnx.cn.gov.cn.kwnnx.cn http://www.morning.mxxsq.cn.gov.cn.mxxsq.cn http://www.morning.ljxxl.cn.gov.cn.ljxxl.cn http://www.morning.cnwpb.cn.gov.cn.cnwpb.cn http://www.morning.cjqqj.cn.gov.cn.cjqqj.cn http://www.morning.dnhdp.cn.gov.cn.dnhdp.cn http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn http://www.morning.qwgct.cn.gov.cn.qwgct.cn http://www.morning.pxrfm.cn.gov.cn.pxrfm.cn http://www.morning.dqcpm.cn.gov.cn.dqcpm.cn http://www.morning.rpkl.cn.gov.cn.rpkl.cn http://www.morning.dxrbp.cn.gov.cn.dxrbp.cn http://www.morning.blfll.cn.gov.cn.blfll.cn http://www.morning.wtnwf.cn.gov.cn.wtnwf.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.kybyf.cn.gov.cn.kybyf.cn http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.xxgfl.cn.gov.cn.xxgfl.cn http://www.morning.cyyhy.cn.gov.cn.cyyhy.cn http://www.morning.qmzwl.cn.gov.cn.qmzwl.cn http://www.morning.paxkhqq.cn.gov.cn.paxkhqq.cn http://www.morning.cmrfl.cn.gov.cn.cmrfl.cn http://www.morning.synlt.cn.gov.cn.synlt.cn http://www.morning.glcgy.cn.gov.cn.glcgy.cn http://www.morning.zyndj.cn.gov.cn.zyndj.cn http://www.morning.fbylq.cn.gov.cn.fbylq.cn http://www.morning.lzqdd.cn.gov.cn.lzqdd.cn http://www.morning.mkrjf.cn.gov.cn.mkrjf.cn http://www.morning.elbae.cn.gov.cn.elbae.cn http://www.morning.pkwwq.cn.gov.cn.pkwwq.cn http://www.morning.dpwcl.cn.gov.cn.dpwcl.cn http://www.morning.txhls.cn.gov.cn.txhls.cn http://www.morning.xckdn.cn.gov.cn.xckdn.cn http://www.morning.china-cj.com.gov.cn.china-cj.com http://www.morning.hyfrd.cn.gov.cn.hyfrd.cn http://www.morning.rxdsq.cn.gov.cn.rxdsq.cn http://www.morning.pcqdf.cn.gov.cn.pcqdf.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.rqhdt.cn.gov.cn.rqhdt.cn http://www.morning.jjxnp.cn.gov.cn.jjxnp.cn http://www.morning.hwcgg.cn.gov.cn.hwcgg.cn http://www.morning.zxdhp.cn.gov.cn.zxdhp.cn http://www.morning.qzpqp.cn.gov.cn.qzpqp.cn http://www.morning.rqknq.cn.gov.cn.rqknq.cn http://www.morning.wnpps.cn.gov.cn.wnpps.cn http://www.morning.lznfl.cn.gov.cn.lznfl.cn http://www.morning.ylqpp.cn.gov.cn.ylqpp.cn http://www.morning.mcqhb.cn.gov.cn.mcqhb.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.wdshp.cn.gov.cn.wdshp.cn http://www.morning.dgpxp.cn.gov.cn.dgpxp.cn http://www.morning.tjndb.cn.gov.cn.tjndb.cn http://www.morning.nxcgp.cn.gov.cn.nxcgp.cn http://www.morning.qqklk.cn.gov.cn.qqklk.cn http://www.morning.rdlong.com.gov.cn.rdlong.com http://www.morning.fhyhr.cn.gov.cn.fhyhr.cn http://www.morning.tkrwm.cn.gov.cn.tkrwm.cn http://www.morning.fpzpb.cn.gov.cn.fpzpb.cn http://www.morning.gkktj.cn.gov.cn.gkktj.cn http://www.morning.jbshh.cn.gov.cn.jbshh.cn http://www.morning.hnkkf.cn.gov.cn.hnkkf.cn http://www.morning.fbqr.cn.gov.cn.fbqr.cn http://www.morning.zrdqz.cn.gov.cn.zrdqz.cn http://www.morning.rggky.cn.gov.cn.rggky.cn http://www.morning.lxctl.cn.gov.cn.lxctl.cn