测试公共 API 是否足以对 API 组件进行单元测试?

发布于 2024-08-05 09:05:22 字数 166 浏览 2 评论 0原文

我的软件(应用程序)使用 httplistener 公开 API(Web 服务)。 从单元测试的角度来看,通过Web请求测试API功能是否足够?

我问这个是因为我读到最佳实践是仅测试类的公共方法。在本例中,我不是测试类的公共方法,而是测试整个组件的公共 API 函数。

感谢您的任何建议

My software (application) exposes an API (webservices) using httplistener.
Is it from a unit test point of view enough to test the API functions through web requests?

I ask this since I read that a best practice is to test only the public methods of a class. In this case I'm not testing public methods of a class but the public API functions from the whole component.

Thanks for any advice

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

残花月 2024-08-12 09:05:23

这更像是集成测试。单元测试将直接测试类的实际方法。如果您在 Web 服务的 webmethods 中有代码,则应该将该代码提取到某些服务类中,这将使它们更易于测试。 Webmethods 本身不应该做任何事情,就像 MVC 中的控制器一样。

不过,你是对的,你应该只需要测试公共方法。无论如何,任何未被公共方法直接或间接使用的私有方法都应该被删除。

That would be more like an integration test. Unit tests would be testing the actual methods on classes directly. if you've got code in webmethods on web services, you should pull that code out into some service classes, which will make them easier to test. Webmethods should never do anything themselves, much like controllers in MVC.

You're right you should only need to test public methods, though. Any private method not used by a public one directly or indirectly, should be deleted anyway.

飘逸的'云 2024-08-12 09:05:23

这取决于你有多少时间。构建 API 后,很好地测试 API 函数非常重要。

但是,如果测试失败,您能否快速找到错误所在?对所有公共方法(即使它们没有由 API 公开)进行单元测试的目的是能够快速纠正您可能因更改而产生的任何错误。

It depends on how much time you have. Once you're building an API, it's really important that you test your API functions very well.

But, if a test fails, can you find quickly where the bug is? The point of unit testing all your public methods (even if they're not exposed by your API) is to be able to quickly correct any bug you might create by changing.

夜巴黎 2024-08-12 09:05:23

“我问这个问题是因为我读到最佳实践是仅测试类的公共方法。在这种情况下,我不是测试类的公共方法,而是测试整个组件的公共 API 函数。

我看不出你在吹毛求疵。公共 API 是公共 API。

类的公共 API 方法或组件中的公共 API 函数是同一件事——公共 API——你唯一应该测试的东西你

有两个级别的公共 API:类级别和组件级别。这意味着你有两个级别的单元测试和组件级别的单元测试

。有些人会声称“单元”始终是一个独立的类,仅此而已,但有些人会声称它

原子或不可分割的。当您的测试不反映单元的结构时,它就是一个单元。

"I ask this since I read that a best practice is to test only the public methods of a class. In this case I'm not testing public methods of a class but the public API functions from the whole component.
"

I can't see the hair you're splitting. Public API is the public API.

Public API methods of a class or Public API functions from a component is the same thing -- a Public API -- the only thing you should test.

You have two levels of public API: class level and component level. That says you have two levels of unit testing. Class-level unit tests and component level unit tests.

Some folks will quibble on the definition of "unit" for unit testing. Some will claim that "unit" is always a stand-alone class and nothing more. I can't see how this is true, but some folks will claim it.

A unit is atomic or indivisible. It's a unit when your test does not reflect the unit's structure.

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