简体   繁体   English

单元测试与集成测试及其维护

[英]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: 实际上,您通常使用各种级别的自动化测试:

  1. Smoke tests. 烟雾测试。 This are unit tests that test various part of the system in the most basic scenarios. 这是在最基本的方案中测试系统各个部分的单元测试。 They are usually employed as part of a gated check-in that don't check in bad code. 它们通常用作不检入错误代码的门禁检入的一部分。 You need this to be fast. 您需要快速。
  2. Regression - unit tests. 回归-单元测试。 Usually part of continuous integration. 通常是持续集成的一部分。 Again you need this to be as fast as possible so that the build will not take too long. 同样,您需要尽可能快地这样做,以便构建不会花费太长时间。
  3. Full regression + integration tests. 全面回归+集成测试。 These are more system tests that take longer to run. 这些是更多的系统测试,需要更长的运行时间。 These usually run once a day in a nightly build or even less frequently (depending on length) 这些通常每天晚上运行一次,甚至更不频繁(取决于长度)

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.删除以前编写的单元测试或留下一些用于文档。

  1. Code coverage cannot be measured with integration testing(as it passes through different systems.)代码覆盖率不能用集成测试来衡量(因为它通过不同的系统。)
  2. Eventhough mainiting both unit tests and integration tests are ideal theoretically let us not risk it says the experienced.尽管维护单元测试和集成测试在理论上是理想的,但让我们不要冒险,有经验的人说。
  3. Only an advanced unit tester can become integration tester.只有高级单元测试员才能成为集成测试员。 So only those who have become integration testers can advice against unit testing.所以只有那些已经成为集成测试人员的人才可以反对单元测试。 We better listen to them because they are passionate about unit testing and are willing to sacrificing the 'fun' of writing unit tests for better 'safety.net' for the team.我们最好听听他们的意见,因为他们对单元测试充满热情,并且愿意牺牲编写单元测试的“乐趣”来为团队提供更好的“safety.net”。
  4. Integration tests are difficult to implement (seeding DB, infra recreation,etc) and we simply wont have time to maintain both(unit and intgration tests)集成测试很难实施(播种数据库、基础设施等),我们根本没有时间同时维护(单元和集成测试)
  5. Unit testing can still be good for library(.dll), framework development, complex calculations and also for product development companies.单元测试对于库(.dll)、框架开发、复杂计算以及产品开发公司仍然有好处。 But how many of us work on these?但是我们当中有多少人从事这些工作? These days for web develoment, everyone can work in end-to-end scenarious easily as there are frameworks already available.如今,对于 web 开发,每个人都可以轻松地在端到端场景中工作,因为已经有可用的框架。 For this integration tests are best anyways.对于这个集成测试无论如何都是最好的。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM