我是单元测试还是集成测试? [英] Am I unit testing or integration testing?

查看:33
本文介绍了我是单元测试还是集成测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用自动化测试,我想测试我的一种数据访问方法.如果数据库没有返回记录,我正在尝试测试代码的作用.

I am starting out with automated testing and I would like to test one of my data access methods. I am trying to test what the code does if the database returns no records.

这是应该在单元测试还是集成测试中完成的事情?

Is this something that should be done in a unit test or an integration test?

谢谢

推荐答案

如果你的测试代码连接到一个实际的数据库并依赖于某些数据的存在(或缺少数据)来通过测试,它是一个集成测试.

If your test code connects to an actual database and relies on the presence of certain data (or lack of data) in order for the test to pass, it's an integration test.

我通常更喜欢通过模拟数据访问方法"用于获取实际数据的组件来测试类似的东西,无论是 JDBC 连接还是 Web 服务代理或其他任何东西.使用mock,你说当这个方法被调用时,返回这个"或者确保这个方法被调用N次",然后你告诉被测类使用mock组件而不是真正的组件.这是一个单元测试",因为您正在测试被测类的行为方式,在一个封闭的系统中,您已经准确地声明了其他组件的行为方式.您已经完全隔离了被测类,并且可以确保您的测试结果不会不稳定并依赖于另一个组件的状态.

I ususally prefer to test something like this by mocking out the component that the "data access method" used to get the actual data, whether that's a JDBC connection or web service proxy or whatever else. With a mock, you say "when this method is called, return this" or "make sure that this method is called N times", and then you tell the class under test to use the mock component rather than the real component. This then is a "unit test", because you are testing how the class under test behaves, in a closed system where you've declared exactly how the other components will behave. You've isolated the class under test completely and can be sure that your test results won't be volatile and dependent on the state of another component.

不确定您使用的是哪种语言/技术,但在 Java 世界中,您可以为此目的使用 JMock、EasyMock 等.

Not sure what language/technology you are working with, but in the Java world, you can use JMock, EasyMock, etc for this purpose.

这篇关于我是单元测试还是集成测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆