我如何测试发生变化的东西?

发布于 2024-12-14 10:47:58 字数 137 浏览 0 评论 0原文

我正在测试一个脚本,在给定一些参数的情况下,该脚本将数据从我们的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朦胧时间 2024-12-21 10:47:58

您正在触及单元测试的非常重要的方面之一:它迫使您编写“好的”代码。如果您希望您的代码在您所描述的情况下能够成功测试,您必须使用控制反转关注点分离

换句话说,要编写成功的测试,您需要抽象出依赖项,最好是接口。然后,您可以通过注入 存根模拟 该接口的实现,它具有可重复且可控的输出。通过这样做,您仅测试被测单元的逻辑(行为或状态,取决于喜好),而不是依赖项。它迫使你分离关注点。

(我会给出示例,但您没有指定您使用的语言)

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)

病女 2024-12-21 10:47:58

我并不是想成为一个自作聪明的人,但是,如果你没有明确的期望,那么你在测试中就没有什么可断言的。

测试所有应该存在的东西。如果您不关心输出的某些部分,只需测试它是否存在即可。如果输出中的某些内容应该存在相关性,请进行测试。

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.

故事和酒 2024-12-21 10:47:58

你的答案已经开始了。要测试脚本,您需要一个测试框架,该框架允许您向脚本提供已知的输入状态、捕获输出并检查输出的正确性。

目前尚不清楚“这个”(不断变化的)所指对象是什么。 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文