Unit Test and Integration Test

Background

It is the first time that I try to write an article in English.

In the past, I didn’t write test code. Just thinking QA is responsible for testing.
As a developer, I don’t need to care about tests.
Although I know tests are essential, I can’t be aware of their importance.

After I joined my current team, it is required for developers to write tests.
So I have to write tests code.

After 8 months, I realize that tests are critical. I need to enforce my ability of testing.

therefore, I recently read a book Vladimir Khorikov - Unit Testing Principles Practices and Patterns.
Learned a lot of unit test and integration best practice and code design.

So I want to share some knowledge with you.

Test

There are two kinds of tests.

  • Unit Test
  • Integration Test
    • end to end test

Test Coverage

Do we need a 100% test coverage?

No. We don’t.

For trivial code, we can ignore that. Because they aren’t worth it.
We should focus on our business logic.

If a metric shows that there’s too little coverage in your code base—say, only 10%—
that’s a good indication that you are not testing enough.

But the reverse isn’t true:
even 100% coverage isn’t a guarantee that you have a good-quality test suite. A test
suite that provides high coverage can still be of poor quality.

Unit Test

The goal of unit test is to enable sustainable project growth.

Just as all tests are not created equal, not all parts of your code base are worth the
same attention in terms of unit testing.

Unit test should test a unit of behavior rather than code.

The four pillars of a good unit test

  • Protection against regressions
  • Resistance to refactoring
  • Fast feedback
  • Maintainability

Protection against regressions

The more features you develop, the more chances there are that you’ll break one of those features with a new release.

Sometimes We don’t awake that the new release will break one existing feature. Even with QA regression test, it also happens many times.

If we have a good automation test, can reduce these situations.

So good tests should protect against regressions.

To maximize the metric of protection against regressions, the test needs to aim at exercising as much code as possible.

Resistance to refactoring

Resistance to refactoring — the degree to which a test can sustain a refactoring of the underlying application code without turning red (failing).

This attribute can give us confidence to refactor.

How can we do?

We shouldn’t test the details of code. We should test the observable behavior.
Aim at the end result instead of implementation details

There is a example:

# we shouldn't care what's detail that we get from the data
test "get user by id" doassert "select * from users where id = 1" == User.get(1).to_sql
end# we just need to verify the data
test "get user by id" doassert User.get(1).name == "Steven"assert User.get(1).id == 1
end

Fast feedback

Fast feedback brings a excellent experience.
Picture the situation, we run a test, it takes 1 minute to show you result.
I can’t stand it.

Our project has hundreds of thousands of code. Run slowly and waste my time to wait for result.

How to run faster?

With less communications of out of process.

Async and parallel execution.

Maintainability

Using plain English as test titles.

Simple phrases in plain English do a much better job: they are more expressive
and don’t box you in a rigid naming structure. With simple phrases, you can describe
the system behavior in a way that’s meaningful to a customer or a domain expert.

how to write code to easy test

Split business logic and out of process communications (side effects).

def check doif User.admin? do:errorelse:ok  end
enddef check_with_side_effect doif User.admin? do# side effectAuditLog.record():errorelse:ok  end
end

With side effects, it is hard to compose code and hard to test.
Pure function is easiest to test.

Integration Test

Using integration tests to verify the behavior of the system as a whole.

Mock

Integration test verifies database, third-party api, mq and so on.

All out-of-process dependencies fall into two categories:

  • Managed dependencies (out-of-process dependencies you have full control over)—These
    dependencies are only accessible through your application; interactions with
    them aren’t visible to the external world. A typical example is a database. External systems normally don’t access your database directly; they do that through
    the API your application provides.
  • Unmanaged dependencies (out-of-process dependencies you don’t have full control over)—
    Interactions with such dependencies are observable externally. Examples include
    an SMTP server and a message bus: both produce side effects visible to other
    applications.

For those out-of-process dependencies, we should mock unmanaged dependencies.

Roles of Test

  • Trivial code: we shouldn’t test, it isn’t worth it.
  • Domain model , algorithms: Unit test carefully test
  • Controllers: Integration test, but doesn’t need to test all situation. Happy path and edge cases are enough.
  • Overcomplicated code: We should reduce these code. Split it.

Test Pyramid

Recap

It simply summary Unit Testing.
Talk is cheap, we should write more code.

Unit Test and Integration Test相关推荐

  1. Unit Test、Integration Test、Regression Test 、Acceptance Test

    Unit test(单元测试):针对每一个单独的代码块进行测试.当它失败的时候会告诉你你的哪块代码有问题需要去修复. Integration test(集成测试):将多个模块结合在一起进行测试,确保多 ...

  2. 论机电一体化的发展By integration of machinery development

    论机电一体化的发展 By integration of machinery development 现代科学技术的不断发展,极大地推动了不同学科的交叉与渗透,导致了工程领域的技术革命与改造,在机械工程 ...

  3. 企业不要求工程师资格认证_谁说工程师不能成为企业家?

    企业不要求工程师资格认证 by Preethi Kasireddy 通过Preethi Kasireddy 谁说工程师不能成为企业家? (Who says engineers can't become ...

  4. Galaxy 生信平台(一):安装

    Galaxy Project( https://galaxyproject.org/)是在云计算背景下诞生的一个生物信息学可视化分析开源项目. 该项目由美国国家科学基金会(NSF).美国国家人类基因组 ...

  5. rails 数据库相关操作命令

    rails 数据库相关操作命令 2013-12-21 16:11 rake db:abort_if_pending_migrations # Raises an error if there are ...

  6. 软件测试工程师面试问题

    有什么原因让你参与到测试和质量管理中来? 什么是测试生命周期,解释一下它的各个阶段? 测试和质量保证有什么不同? 什么是Negative测试? 在之前做测试的过程总遇到过哪些问题?你是如何解决的? 你 ...

  7. 领先微软技术咨询公司招聘技术人员

    Avanade是全球领先的微软技术咨询公司,在中国正处于起步并快速发展的阶段,现正招聘以下职位,有兴趣者可以将中英文简历发送至lin.a.zhu@avanade.com 或 linuszhu@gmai ...

  8. Domain Driven Design and Development In Practice--转载

    原文地址:http://www.infoq.com/articles/ddd-in-practice Background Domain Driven Design (DDD) is about ma ...

  9. 机器学习结构化学习模型_生产化机器学习模型

    机器学习结构化学习模型 The biggest issue in the life-cycle of ML project isn't to create a good algorithm or to ...

最新文章

  1. 在ASP.NET MVC中使用Log4Net记录异常日志,出错时导向到静态页
  2. mysql事务所_MySQL事务,这篇文章就够了
  3. access中总计为first_用Access开发生产管理系统
  4. 好代码实践:基于Redis的轻量级分布式均衡消费队列
  5. 关于java.lang.ArithmeticException: divide by zero的异常
  6. 职场精英修炼必学工具:75页自我管理的29个工具,系统方法论
  7. 视图查询sql_视图 索引
  8. 电脑必备必装的软件工具神器,强烈推荐
  9. 怎么完全卸载赛门铁克_如何干净彻底卸载诺顿?
  10. c语言反向工程软件,逆向工程工具之Reverse
  11. ASP.NET MVC 5高级编程 (pdf书)
  12. OCP 创建可插拔数据库PDB
  13. 网站备案其实是服务器备案,网站备案指的是备案域名还是备案主机空间
  14. 苹果开发者账号申请流程
  15. 设计模式系列:搞懂装饰器模式,增加自身技能
  16. 自定义AVA数据集流程及在SlowFast中训练
  17. 自动生成企业画像(标签)
  18. 自媒体文章一文多发的几种方式
  19. 走进JavaWeb技术世界5:初探Tomcat的HTTP请求过程
  20. CP2102官方驱动安装后无作用

热门文章

  1. JAVA随机生成6位数,不足补0
  2. Halcon17完美破解兼容17.12.0.0附破解说明
  3. 中级微观经济学:Chap 14 消费者剩余
  4. 129.s1-拦截黑名单的电话,Android Studio使用aidl
  5. 让职场人心惊胆战的职场“黑话大全” 您知多少?
  6. 见闻-关于codec芯片
  7. 系统开机进入boot menu app menu解决办法
  8. dlopen和dlsym
  9. bmi计算 python_《Python之BMI计算》
  10. Enow ICQ上的自白