为什么在单元测试中测试不应该依赖于执行的顺序? [英] Why in unit testing tests should not depend on the order of execution?

查看:28
本文介绍了为什么在单元测试中测试不应该依赖于执行的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们经常认为单元测试不应该依赖于测试顺序.我认为这不是一个确切的规则,而是建议.

It is a very often opinion that unit testing should not rely on tests order. I think it is not an exact rule but recommendation.

但在某些情况下它看起来并不好.例如,我有 CUtility 类和使用 CUtility 方法的 CSoket.我想在单个单元测试执行中运行测试.所以逻辑上先运行 CUtility 测试,然后运行 ​​CSoket 测试.

But in some cases it does not look good. For example I have CUtility class and CSoket, which uses CUtility methods. I want to run tests in a singe unit test execution. So it is logically to run CUtility test first and after that run CSoket tests.

但是单元测试的最佳实践说:不要依赖测试顺序.为什么?

But best practice of unit testing says: Do not rely on tests order. Why?

推荐答案

因为单元测试旨在孤立地测试单个工作单元.CSoket 中的 Cutility 方法调用应该在 CSoket 的单元测试中模拟.这样,CSoket 的单元测试就独立于 CUtility 测试通过或失败.那么,先运行 CUtility 还是 CSoket 测试就不再重要了.

Because unit tests are meant to test individual units of work in isolation. Cutility method calls in CSoket should be mocked in unit tests for CSoket. This way, the unit tests for CSoket become independent of CUtility tests passing or failing. Then, it no longer matters if CUtility or CSoket tests are run first.

这篇关于为什么在单元测试中测试不应该依赖于执行的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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