写好unit test的建议和例子

单元测试是确保代码质量的重要手段,其目标在于发现缺陷、提供调用示例及支持重构。有效的找bug方式包括集成测试和手动测试。良好的单元测试应遵循 Arrange-Act-Assert 结构,验证应用的事实而非精确结果,独立于外部服务和状态,并避免不必要的预条件。测试异常处理和多种输入值或状态也很关键。测试用例应清晰、一致地命名。
摘要由CSDN通过智能技术生成

最近翻了下写unit test 的文章,总结如下

What’s unit test?

“Unit testing is a software testing method by which individual units of source code.” – –Wikipedia

What’s integration test?

“Integration testing is the phase in software testing in which individual software modules are combined and tested as a group” – Wikipedia

Goal for unit test

  • Defects obvious bugs
  • Provide an example about how to call it
  • Refactor

Effective way to find bugs

  • Integration test
  • Manual test

How to write good unit test?

Arrange -> Act -> Assert
# zoo.py
class Zoo:

    def __init__(self, animals):
        self.animals = animals

    def sort_by_name(self):
        self.animals = sorted(self.animals)

    def get_animals(self):
        return self.animals

# test_zoo.py
import unittest


class Zoo:

    def __init__(self, animals):
        self.animals = animals

    def
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值