[英]unit testing vs integration testing & its maintenance
I am working on a web application which is constantly enhancing in parallel development environment(develop two requirements in two different environments and merge first code base to second, when first requirement is released to production).我正在开发一个 web 应用程序,它在并行开发环境中不断增强(在两个不同的环境中开发两个需求,并在第一个需求发布到生产环境时将第一个代码库合并到第二个代码库)。
My question is about having both integrating testing and unit testing for app and its maintenance.我的问题是关于对应用程序及其维护进行集成测试和单元测试。
Unit testing with mocking makes difficult to maintain tests in parallel development, integration testing(using selenium) in parallel development makes difficult to maintain required data in database(which may be easy than fixing a failed unit test)使用 mocking 进行单元测试很难在并行开发中维护测试,并行开发中的集成测试(使用 selenium)很难在数据库中维护所需的数据(这可能比修复失败的单元测试容易)
I am leaning towards integration testing, as merging code will not break use case, but unit test case may fail by merging code because of expectations.我倾向于集成测试,因为合并代码不会破坏用例,但单元测试用例可能会因为预期而通过合并代码而失败。 The app is little old and not properly designed For unit Testing and refactoring code and maintaining unit test cases is becoming hard.Please suggest a better approach for testing.该应用程序有点旧且设计不当对于单元测试和重构代码以及维护单元测试用例变得越来越困难。请提出更好的测试方法。
Unit tests and integration tests both have their place. 单元测试和集成测试都有自己的位置。 Unit tests, as the name indicates, verifies the unit is behaving as expected. 顾名思义,单元测试可验证单元的行为是否符合预期。 It's true that integration tests cover the same code that is covered by the unit tests. 确实,集成测试涵盖了单元测试所涵盖的相同代码。 But unit tests help you pin point issues more easily. 但是单元测试可以帮助您更轻松地找出问题所在。 Instead of investigating the failure to understand which part of the system is responsible for the issue - you have a failing unit test to help you find out. 您不必调查无法理解系统的哪个部分导致问题的方法,而是可以通过失败的单元测试来发现问题。 Another reason to have unit tests is speed. 进行单元测试的另一个原因是速度。 Unit tests should be fast. 单元测试应该很快。 They should not rely on various system dependencies (you should be using mocks and stubs for that). 它们不应依赖于各种系统依赖性(为此,您应该使用模拟和存根)。 If you have unit tests with good coverage you get feedback fast regarding the quality of the system - before you start your long test cycle. 如果您的单元测试覆盖范围广,那么在开始漫长的测试周期之前,您会快速获得有关系统质量的反馈。
Actually you usually employ various level of automated tests: 实际上,您通常使用各种级别的自动化测试:
If you find the cost of maintaining some types of tests too high, it's sensible to consider dropping them. 如果您发现维护某些类型的测试的成本过高,则考虑删除它们是明智的。 I think integration tests are more important for QA, and if I could only choose one type of testing (unit vs integration) that's what I would go with. 我认为集成测试对于质量检查更为重要,如果我只能选择一种测试类型(单元还是集成),那将是我想要的。
However, first you might also want to consider if there is a way to factor your unit tests to avoid these maintenance issues. 但是,首先,您可能还想考虑是否有一种方法可以将您的单元测试作为因素来避免这些维护问题。 When I first started using mocks, it took me a while to find the right balance. 刚开始使用模拟游戏时,我花了一段时间才找到合适的平衡点。 In my experience I find it best to avoid mocks (with expectations) as much as much as possible. 以我的经验,我发现最好尽可能避免模拟(有期望)。 I do use mocking libraries, though, just mostly for trivial stubbing rather than more complex mocking. 但是,我确实使用了模拟库,主要是为了进行简单的存根而不是更复杂的模拟。 I wrote a blog post about this a while back if you are interested. 如果您有兴趣,我不久前写了一篇关于此的博客文章 。
W/ regard to the unit test issue: 关于单元测试问题:
I would suspect that the unit tests you are constantly having to refactor are a bit too low of a level. 我怀疑您经常需要重构的单元测试水平太低了。 Generally it is better to have somewhat higher level tests that exercise the lower level code by varying the inputs. 通常,最好有一些较高级别的测试通过改变输入来行使较低级别的代码。
Assuming there is not unnecessary code, the higher level tests should still provide good code coverage (if they can't reach code, why is the code there?). 假设没有不必要的代码,那么较高级别的测试仍应提供良好的代码覆盖率(如果无法到达代码,为什么代码在那里?)。
W/ regard to the functional test issue: 关于功能测试问题:
You may want to consider a representative data sample (or several). 您可能需要考虑一个或多个代表性数据样本。 That way you can have a known input, so you should be get predictable output. 这样,您可以拥有一个已知的输入,因此您应该获得可预测的输出。
Avoid unit testing altogether.完全避免单元测试。 Or start with Unit testing and once you reach integration tests "pass the relay baton" to integration testing.或者从单元测试开始,一旦达到集成测试,“将接力棒传递给集成测试”。 Delete the unit tests previously written or leave some for documentation.删除以前编写的单元测试或留下一些用于文档。
Some helpful links:一些有用的链接:
"Why You Need to Stop Writing Unit Tests" https://hackernoon.com/why-you-need-to-stop-writing-unit-tests “为什么你需要停止编写单元测试” https://hackernoon.com/why-you-need-to-stop-writing-unit-tests
"Write tests. Not too many. Mostly integration." “编写测试。不是太多。主要是集成。” https://kentcdodds.com/blog/write-tests https://kentcdodds.com/blog/write-tests
"The No. 1 unit testing best practice: Stop doing it" https://techbeacon.com/app-dev-testing/no-1-unit-testing-best-practice-stop-doing-it “第一单元测试最佳实践:停止做” https://techbeacon.com/app-dev-testing/no-1-unit-testing-best-practice-stop-doing-it
"Unit test kills" https://www.linkedin.com/pulse/before-you-feed-unit-test-beast-sanj-ay-mohan/?trackingId=5eIFIJGSBpnGuXMEz2PnwQ%3D%3D “单元测试杀死” https://www.linkedin.com/pulse/before-you-feed-unit-test-beast-sanj-ay-mohan/?trackingId=5eIFIJGSBpnGuXMEz2PnwQ%3D%3D
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.