我如何测试发生变化的东西?
我正在测试一个脚本,在给定一些参数的情况下,该脚本将数据从我们的 API 复制到 CDN 上的某个位置。
然而,这种情况不断变化。我应该如何为其添加测试?也许向 API 添加一些东西,在给定一些参数时总是返回相同的东西?
谢谢!
I'm looking to test a script that, given some parameters, replicates data from our API to a location on our CDN.
However, this constantly changes. How should I add a test for it? Perhaps add something to the API that always returns the same thing when given some parameters?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在触及单元测试的非常重要的方面之一:它迫使您编写“好的”代码。如果您希望您的代码在您所描述的情况下能够成功测试,您必须使用控制反转和关注点分离。
换句话说,要编写成功的测试,您需要抽象出依赖项,最好是接口。然后,您可以通过注入 存根 或 模拟 该接口的实现,它具有可重复且可控的输出。通过这样做,您仅测试被测单元的逻辑(行为或状态,取决于喜好),而不是依赖项。它迫使你分离关注点。
(我会给出示例,但您没有指定您使用的语言)
You are touching one of the very important aspect of unit-testing: it forces you to write "good" code. If you want your code to be successfully testable in the kind of cases you're describing, you have to use good practices like inversion of control and separation of concerns.
In other words, to write successful tests, you need to abstract out your dependencies, preferably to interfaces. You can then run your tests by injecting a stub or a mocked implementation of that interface, which has reproducible and controllable output. By doing this, you're testing only the logic (behaviour or state, depending on tastes) of your unit under test, and not your dependencies. It forces separation of concerns on you.
(I would give examples but you didn't specify which language you're using)
我并不是想成为一个自作聪明的人,但是,如果你没有明确的期望,那么你在测试中就没有什么可断言的。
测试所有应该存在的东西。如果您不关心输出的某些部分,只需测试它是否存在即可。如果输出中的某些内容应该存在相关性,请进行测试。
I'm not trying to be a smartass but, if you have no defined expectation, you have nothing to assert in the test.
Test everything that should be there. If you don't care about some part of the output, just test it's existence. If there should be a correlation of some things inside the output, test that.
你的答案已经开始了。要测试脚本,您需要一个测试框架,该框架允许您向脚本提供已知的输入状态、捕获输出并检查输出的正确性。
目前尚不清楚“这个”(不断变化的)所指对象是什么。 CDN 上的位置?如果是这样,您需要冻结该位置以进行测试。
You have a start on your answer. To test your script, you need a test framework that allows you to feed a known input state to the script, capture the output, and check the output for correctness.
It's not clear what the referent of "this" (constantly changing) is. The location on the CDN? If so, you'll need to freeze that location for testing.