zoukankan      html  css  js  c++  java
  • 单元测试前篇

    一、什么是单元测试?
    单元测试的本质:写代码去测试代码。
     
    二、原始单元测试的方式
    1、一个加法的代码
    class Count(object):

    def __init__(self,a,b):
    self.a=a
    self.b=b

    def add(self):
    return self.a+self.b
    2、一个测试类来测试该加法代码

    from
    caculate import Count

    class TestCount(object):

    def test_count(self):
    try:
    j=Count(1,2)
    assert(j.add()==3),"Integer addtion result error!"
    except AssertionError as msg:
    print(msg)
    else:
    print("test pass!")

    test_count=TestCount()
    test_count.test_count()

      3、运行代码

        当运行正确,结果如下:test pass!

        修改:assert(j.add()==2),"Integer addtion result error!",继续运行代码,结果为:Integer addtion result error!

    总结,以上就是一个单元测试的过程;写代码测试代码。

    虽然,上面的代码测试代码完成了我们所需要的结果;但是存在许多的缺陷,比如:不同的程序员写代码的风格不一致,导致了我们维护代码的成本比较高;为了解决以上问题,我们引入了单元测试框架的概念。

    那么,什么是单元测试框架呢?

      

     
  • 相关阅读:
    C89:论内存泄漏
    C++03:模板
    C++的STL(标准模板库)系列:容器——string容器
    C++03:论类的友元函数和内联函数
    C++03:论类的运算符重载
    Windows开发:网络编程基础
    Windows开发:论文件和权限
    C89:头文件
    C89:论符号
    纪录片(深度好片)
  • 原文地址:https://www.cnblogs.com/pengwa1226/p/12776084.html
Copyright © 2011-2022 走看看