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

    The Art of Unit Testing with Examples in .NET

    第一章:单元测试的基本知识

    0、一个好的单元测试应当有如下特性:

    ❂ It should be automated and repeatable.
    ❂ It should be easy to implement.
    ❂ Once it’s written, it should remain for future use.
    ❂ Anyone should be able to run it.
    ❂ It should run at the push of a button.
    ❂ It should run quickly.

    1、写单元测试的传统方式

    2、TDD中的单元测试开发

    3 TDD开发

    1 Write a failing test to prove code or functionality is missing from the end product.

    2 Make the test pass by writing production code that meets the expectations of your test.

    3 Refactor your code.

    第二章:单元测试举例

    1、基本的命名规范:

    Project:[项目名].Tests,如:AOUT.Logan.Tests

    Class:[类名]Tests,如: LogAnalyzerTests

    Method:[方法名]_[测试条件]_[在该测试条件下返回的结果],如:IsValidFileName_validFile_ReturnsTrue()

    2 示例:

    using System;

    using System.IO;

    namespace AOUT.CH2.LogAn

    {

    public class LogAnalyzer

    {

    public bool IsValidLogFileName(string fileName)

    {

    if (!File.Exists(fileName))

    {

    throw new Exception("No log file with that name exists");

    }

    if(!fileName.ToLower().EndsWith(".slf"))

    {

    return false;

    }

    return true;

    }

    }

    }

    测试代码

    using System;

    using NUnit.Framework;

    namespace AOUT.CH2.LogAn.Tests

    {

    [TestFixture]

    public class LogAnalyzerTests

    {

    private LogAnalyzer m_analyzer=null;

    [SetUp]

    public void Setup()

    {

    m_analyzer = new LogAnalyzer();

    }

    [Test]

    [Ignore("This test is broken")]

    public void IsValidFileName_validFileLowerCased_ReturnsTrue()

    {

    bool result = m_analyzer.IsValidLogFileName("whatever.slf");

    Assert.IsTrue(result, "filename should be valid!");

    }

    [Test]

    public void IsValidFileName_validFileUpperCased_ReturnsTrue()

    {

    bool result = m_analyzer.IsValidLogFileName("whatever.SLF");

    Assert.IsTrue(result, "filename should be valid!");

    }

    [Test]

    [ExpectedException(typeof(Exception),"No log file with that name exists")]

    public void IsValidFileName_nunintvalidFileUpperCased_ReturnsTrue()

    {

    bool result = m_analyzer.IsValidLogFileName("whatever.SLF");

    Assert.IsTrue(result, "filename should be valid!");

    }

    [TearDown]

    public void TearDown()

    {

    m_analyzer = null;

    }

    }

    }

    第三章:用测试桩消除依赖

    1、Refactoring our design to be more testable

    Refactoring is the act of changing the code’s design without breaking
    existing functionality.
    Seams are places in your code where you can plug in different function-
    ality, such as stub classes.

    2、消除依赖的具体技术

    ❂ Extract an interface to allow replacing underlying implementation.
    ❂ Inject stub implementation into a class under test.
    ❂ Receive an interface at the constructor level.
    ❂ Receive an interface as a property get or set.
    ❂ Get a stub just before a method call.

    第四章:用Mock对象进行交互测试

    1 Interaction testing is testing how an object sends input to or receives
    input from other objects—how that object interacts with other objects.

    2 A mock object is a fake object in the system that decides whether the
    unit test has passed or failed. It does so by verifying whether the object
    under test interacted as expected with the fake object. There’s usually
    no more than one mock per test.

    3 A fake is a generic term that can be used to describe either a stub or a mock
    object (handwritten or otherwise), because they both look like the real object.
    Whether a fake is a stub or a mock depends on how it’s used in the current
    test. If it’s used to check an interaction (asserted against), it’s a mock object.
    Otherwise, it’s a stub.

    作者:深潭
    出处:http://www.cnblogs.com/dbasys/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    30道四则运算
    《梦断代码》第0章读书随笔
    《梦断代码》阅读计划
    [记录] Mybatis原理
    GitLab登录密码重置后无法使用idea拉取代码提交代码问题解决
    写邮件和回复邮件
    XMLDocument 方法中实现post发送消息
    愿我温柔如水,坚强如钢!
    第6届蓝桥杯javaA组第7题,牌型种数,一道简单的题带来的思考
    IE兼容性问题解决方案2--css样式兼容标签
  • 原文地址:https://www.cnblogs.com/dbasys/p/1813253.html
Copyright © 2011-2022 走看看