简体   繁体   English

如何编写集成测试?

[英]How to write integration tests?

My team develops a Web API 2 application using Entity framework 6 ORM.我的团队使用实体框架 6 ORM 开发了一个 Web API 2 应用程序。

Our application acts as an email, and it's main purpose and action is to send a letter from one client to another.我们的应用程序充当电子邮件,它的主要目的和作用是从一个客户向另一个客户发送一封信。

Since it's the application's main purpose, we'd all like to have an integration test that will assure us that a letter is sent correctly.由于这是应用程序的主要用途,我们都希望进行集成测试,以确保我们正确发送了一封信件。 That is because we don't want to accidentally insert code that will break that.那是因为我们不想意外插入会破坏它的代码。

When sending a letter the main actions that happen and that we'd like to test are-在发送信件时,发生的主要动作以及我们想要测试的是——

  • A letter is received through HTTP Post method and is validated on server通过HTTP Post方法收到一封信并在服务器上进行验证
  • The letter is saved to the Database .信件被保存到数据库中
  • The letter is serialized.这封信是连载的。
  • The serialized letter is sent to the specified destinations through an external module.序列化的信件通过外部模块发送到指定的目的地。
  • The letter is deserialized on reciever side.该信件在接收方反序列化。
  • The letter is added to recieved client's database.该信件被添加到收到的客户的数据库中。

I'm asking this because I'm clueless about how to approach this, yet I really want that sort of test which I can rely on.我问这个是因为我对如何解决这个问题一无所知,但我真的想要那种我可以依赖的测试。

How should a test for this be set?应该如何设置测试? Where do I start?我从哪里开始? How to achieve an actual letter sending through a test and make sure it works?如何通过测试实现实际的信件发送并确保其有效?

It doesn't have to be an end to end test.它不一定是端到端的测试。

Thanks谢谢

Edit:编辑:

I don't mind tests to take long time, they can be run at night, but I want them to verify as much as possible that the application main parts are working as expected.我不介意测试需要很长时间,它们可以在晚上运行,但我希望他们尽可能多地验证应用程序主要部分是否按预期工作。

I would set up multiple, non-actual code tests to verify your logic:我会设置多个非实际代码测试来验证您的逻辑:

  1. Reception of the letter, and it's logic收到信,这是逻辑
  2. Verify that the save method to the database is invoked only when it should验证对数据库的 save 方法是否仅在应调用时调用
  3. Verify the serialization/deserialization bits of code验证代码的序列化/反序列化位
  4. Verify that the destination picker module is working correctly.验证目标选择器模块是否正常工作。

Once you have these "non-invoking" tests, you know that all the bits of your system are working correctly individually.一旦您进行了这些“非调用”测试,您就会知道系统的所有部分都可以单独正常工作。 You can then set up a unit-test with some kind of marking (ie TestCategory("SUPERSLOW")) and then have that run manually against a mocked set of recipients for verification during integration test runs, NOT as part of your CI.然后,您可以设置带有某种标记(即 TestCategory("SUPERSLOW"))的单元测试,然后在集成测试运行期间针对一组模拟收件人手动运行该单元测试以进行验证,而不是作为 CI 的一部分。

Keywords here are:这里的关键词是:

  • SOLID principles固体原则
  • Separation of Concerns关注点分离
  • Mocking嘲讽
  • Continuous Integration持续集成
  • Inversion of Control控制反转

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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