简体   繁体   English

如何正确单元测试Web应用程序?

[英]How to properly unit test a web app?

I'm teaching myself backend and frontend web development (I'm using Flaks if it matters) and I need few pointers for when it comes to unit test my app. 我正在教自己后端和前端Web开发(我正在使用Flaks,如果它很重要)我需要几点指示单元测试我的应用程序。

I am mostly concerned with these different cases: 我主要关注这些不同的情况:

  1. The internal consistency of the data : that's the easy one - I'm aiming for 100% coverage when it comes to issues like the login procedure and, most generally, checking that everything that happens between the python code and the database after every request remain consistent. 数据的内部一致性 :这很简单 - 当涉及到登录过程等问题时,我的目标是100%覆盖,最常见的是,检查每个请求后python代码和数据库之间发生的所有事情是一致的。

  2. The JSON responses : What I'm doing atm is performing a test-request for every get/post call on my app and then asserting that the json response must be this-and-that , but honestly I don't quite appreciate the value in doing this - maybe because my app is still at an early stage? JSON响应 :我正在做的atm是对我的应用程序上的每个get/post调用执行测试请求,然后断言 json响应必须是这个和那个 ,但老实说我不太欣赏它的价值这样做 - 也许是因为我的应用还处于早期阶段?

    • Should I keep testing every json response for every request? 我应该继续测试每个请求的每个json响应吗?
    • If yes, what are the long-term benefits? 如果是,那么长期利益是什么?
  3. External APIs : I read conflicting opinions here. 外部API :我在这里阅读了相互矛盾的观点。 Say I'm using an external API to translate some text: 假设我正在使用外部API来翻译某些文本:

    • Should I test only the very high level API, ie see if I get the access token and that's it? 我应该只测试非常高级别的API,即看看我是否获得了访问令牌,那就是它?
    • Should I test that the returned json is what I expect? 我应该测试返回的json是否符合预期?
    • Should I test nothing to speed up my test suite and don't make it dependent from a third-party API? 我应该测试什么来加速我的测试套件并且不依赖于第三方API吗?
  4. The outputted HTML : I'm lost on this one as well. 输出的HTML :我也迷失了这个。 Say I'm testing the function add_post() : 假设我正在测试函数add_post()

    • Should I test that on the page that follows the request the desired post is actually there? 我应该在请求后面的页面上测试,所需的帖子实际上在那里吗?
    • I started checking for the presence of strings/html tags in the row response.data , but then I kind of gave up because 1) it takes a lot of time and 2) I would have to constantly rewrite the tests since I'm changing the app so often. 我开始检查行response.data是否存在字符串/ html标签,但后来我放弃了因为1)它需要花费很多时间而且2)我必须不断重写测试,因为我正在改变应用程序经常这样。
    • What is the recommended approach in this case? 在这种情况下,推荐的方法是什么?

Thank you and sorry for the verbosity. 谢谢你,并为这些冗长而感到遗憾。 I hope I made myself clear! 我希望自己清楚明白!

Most of this is personal opinion and will vary from developer to developer. 其中大多数是个人意见,因开发人员而异。

  1. There are a ton of python libraries for unit testing - that's a decision best left to you as the developer of the project to find one that fits best with your tool set / build process. 有大量的python库可用于单元测试 - 作为项目开发人员,最好留给您一个最适合您的工具集/构建过程的决策。

  2. This isn't exactly 'unit testing' per se, I'd consider it more like integration testing. 这本身并不完全是“单元测试”,我认为它更像是集成测试。 That's not to say this isn't valuable, it's just a different task and will often use different tools. 这并不是说这不是有价值的,它只是一个不同的任务,并且经常使用不同的工具。 For something like this, testing will pay off in the long run because you'll have piece of mind that your bug fixes and feature additions aren't impacting your end to end code. 对于类似这样的事情,从长远来看,测试将获得回报,因为您可以放心,您的错误修复和功能添加不会影响您的端到端代码。 If you're already doing it, I would continue. 如果你已经这样做了,我会继续。 These sorts of tests are highly valuable when refactoring down the road to ensure consistent functionality. 在重构道路以确保一致的功能时,这些类型的测试非常有价值。

  3. I would not waste time testing 3rd party APIs. 我不会浪费时间测试第三方API。 It's their job to make sure their product behaves reliably. 他们的工作是确保他们的产品行为可靠。 You'll be there all day if you start testing 3rd party features. 如果你开始测试第三方功能,你会整天待在那里。 A big reason to use 3rd party APIs is so you don't have to test them. 使用第三方API的一个重要原因是您不必测试它们。 If you ever discover that your app is breaking because of a 3rd party API it's probably time to pick a different API. 如果您发现您的应用因第三方API而中断,那么可能是时候选择不同的API了。 If your project scales to a size where you're losing thousands of dollars every time that API fails you have a whole new ball of issues to deal with (and hopefully the resources to address them) at that time. 如果您的项目规模扩大到每次API失败时都会损失数千美元,那么当时就会有一个全新的问题需要处理(并希望能够解决这些问题的资源)。

  4. In general, I don't test static content or html. 一般来说,我不测试静态内容或HTML。 There are tools out there (web scraping tools) that will let you troll your own website for consistent functionality. 有一些工具(网络抓取工具),可以让你自己的网站,以获得一致的功能。 I would personally leave this as a last priority for the final stages of refinement if you have time. 如果你有时间,我个人会把这作为最后阶段的最后优先考虑。 The look and feel of most websites change so often that writing tests isn't worth it. 大多数网站的外观和感觉经常变化,以至于编写测试并不值得。 Look and feel is also really easy to test manually because it's so visual. 外观也很容易手动测试,因为它是如此直观。

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

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