上午我的单元测试和集成测试? [英] Am I unit testing or integration testing?

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

问题描述

我开始了自动化测试,我想测试我的数据访问方法之一。我想测试,如果数据库返回任何记录的code做了什么。

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?

感谢

推荐答案

如果您的测试code连接到一个实际的数据库,并以依赖于特定数据的presence(或缺乏数据)为测试通过,它是一个集成测试。

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.

我后容易preFER通过嘲讽了组件来测试像这样的用于获取的实际数据,不管是JDBC连接或Web服务代理或任何其他的数据访问方法。使用模拟,你说:当这个方法被调用,返回此或确保这种方法被称为N次,然后你告诉被测类使用模拟组件,而不是真正的组件。然后,这是一个单元测试,因为你正在测试被测类的行为,在你声明正是其他组件将如何表现一个封闭的系统。你已经分离出的类测试并完全可以确保您的测试结果将不会挥发,依赖于其他组件的状态。

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天全站免登陆