扩展 Junit4 或测试用例?

发布于 2024-09-14 03:18:24 字数 660 浏览 7 评论 0原文

我们有一个简单的网络服务来处理有关数据的查询。我想制作一组断言/案例扩展,为测试响应的各个方面提供高级方法。例如,我可以编写assertResultCountMinimum(int)。该方法将负责构建查询、执行查询以及解包响应以验证数据。我还想

确保我对于如何解决这个问题有正确的想法。

首先创建一个我自己的测试用例类,并使用正确的设置和拆卸方法。出于我们的目的,MyTestCase。然后提供一系列使用新的断言方法扩展 Assert 的类。这些类的最终用户将扩展 MyTestCase 并使用我创建的断言。这是我在 jWebUnit 中看到的模式。

我觉得我正在混合和匹配 junit 3 和 4 的概念。我希望只有 junit 4 概念。但我似乎无法在脑海中找到构建此项目的正确方法。另外,属于 Junit 的 Assert 类的断言方法都是静态的。我的一些断言需要重新查询网络服务。这让我觉得我真的应该将断言作为 MytestCase 内部的一系列辅助函数提供。后者完成了工作,但感觉不对。

任何见解、思考、澄清请求,非常感谢。

后续编辑: 正如珍妮在下面建议的那样,我正在创建一个包含我所有断言和断言的超级类。设置/拆卸方法。实际上,我的断言实际上是辅助函数,它包装了基本的 junit 4 断言,我将其导入到我的超类中。我的任何测试都只会扩展这个超级类。我正在考虑的一个警告是使超类抽象,因为不应该有超类的任何实例。

We've got a simple webservice for handling queries about our data. I'd like to make a set of asserts/case extentions that would provide high level methods for testing various aspects of the response. For instance I could write assertResultCountMinimum(int). The method would take care of building the query, executing it, and unpacking the response to validate the data. I'd also like the to

I want to make sure I have the right idea in my head about how to go about this.

First create a test case class of my own, with the right setup and teardown methods. For our purposes, MyTestCase. Then provide a series of classes that extend Assert with the new assert methods. The end user of these classes would extend MyTestCase and would use the asserts that I've created. This is the pattern I think I see in jWebUnit.

I feel like I'm mixing and matching junit 3 and 4 concepts. I'd love to have just junit 4 concepts. But I can't seem to line up in my head the proper way to build this. Also, the assert methods that belong to Junit's Assert class are all static. Some of my asserts would require requerying the webservice. This makes me think I should really just provide the asserts as a series of helper functions inside of MytestCase. The later gets the job done, but doesn't feel right.

Any insight, musings, requests for clarification, much appreciated.

Follow up edit:
As Jeanne suggests below, I'm creating a super class with all of my asserts & setup/teardown methods. In reality my asserts are actually helper functions which wrap around the basic junit 4 asserts, which I import into my super class. Any test of mine will just extend this super class. One caveat that I'm considering is making the super class abstract, since there shouldn't be any instance of the super class.

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

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

发布评论

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

评论(1

日暮斜阳 2024-09-21 03:18:24

马克,
我在 JUnit 4 中使用两种模式。对于“实用程序类型”断言,我创建了一个静态类。例如ReflectionAssertions。然后,我使用静态导入在 JUnit 4 测试中使用这些断言。

对于仅在一个类中使用的本地类型断言,我将它们作为 JUnit 4 测试类本身的常规方法。例如,assertCallingMyBusinessMethodWithNullBlowsUp()。这些没有太大的再利用价值。

我不考虑这种混合概念,因为后面的组在我的测试之外不可重用。如果我有可重用的断言来进行 Web 服务调用(因此需要状态),我将创建一个不扩展 TestCase 的超类并使用它。我的超类将具有用于设置的状态和@Before 方法。因此,它是测试的一部分。

Marc,
I use two patterns in JUnit 4. For "utility type" assertions, I made a static class. For example ReflectionAssertions. Then I use a static import to use those assertions in my JUnit 4 test.

For local type assertions that are only used in one class, I make them regular methods in the JUnit 4 test class itself. For example assertCallingMyBusinessMethodWithNullBlowsUp(). These don't have much reuse value.

I don't consider this mixing concepts because the later group aren't reusable outside my test. If I had reusable assertions that made webservice calls (and therefore needed state), I would create a superclass that did not extend TestCase and use that. My superclass would have the state and @Before methods for setup. As such, it is part of the test.

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