数据测试 / REST Assured / Java 单元测试

huangapple go评论94阅读模式
英文:

Data Testing / REST Assured / Java Unit Testing

问题

过去,我曾经使用过Rest Assured和其他Java框架。

这更像是一个开放性问题,关于如何以最佳/现代的方式测试数据,无论是SQL还是Oracle等等。

所基本的是,您需要确认表中的数据是否等于特定的字符串/整数/布尔值等。测试需要能够通过Maven或可以集成到Jenkins类型的环境中运行,以便在每次构建时运行。

您有什么建议来实现这一点的最佳方式吗?欢迎您提供任何您曾经使用过的工具或想法。

我已经进行了一些搜索,大多数人只是进行正常的单元测试。但如果我想测试应该保持不变的生产数据,您有什么建议或最佳实践吗?

谢谢
Flenters

英文:

So in the past I have worked with rest assured and other java frameworks.

This is more an open ended question in what is the best / modern way to test data either sql / oracle ...etc

So the basics would be is you need to confirm data in a table is equal to a specific string / int / boolean ...etc. The test needs to be able to run via maven or something that can be integrated into a Jenkins type of environment so that it will be run on every build.

What would you suggest is the best way to achieve this any idea or tools that you have used would be welcome.

I have searched around and most people are just doing normal unit tests. But if i wanted to test production data that should remain static what would you suggest or are the best practices.

Thanks
Flenters

答案1

得分: 1

让我陈述一下我的观点,希望能澄清一些问题:

首先,数据测试确实是一个广泛的话题,没有一个"万能解决方案"和一种通用的方法。

通常情况下,你可以使用Spring测试框架来测试你的DAO代码(SQL查询),该框架提供了一种在测试之前加载一些数据、运行与数据库交互的代码、事务管理等功能。

这种测试不是单元测试,它属于集成测试的范畴,可以很容易地通过Maven/Gradle支持,并作为流水线的一部分从Jenkins运行。

还有一个名为DbUnit的工具也在这个领域可用。

然而,这并不等同于"测试数据"。

让我问一个问题,实际上是很多问题,它们都可以归结为:你到底想测试什么? 数据是否一致?你的数据库中是否有约束,你是否依赖于它们?还是为了性能原因或其他任何可以由你的用例合理证明的原因而避免使用它们,例如索引?

你是否打算在测试过程中填充数据?这将如何影响已经存在于生产环境中的数据?

你计划在测试中使用事务吗?例如,我上面提到的Spring测试框架可以在测试之前自动打开一个"测试事务",并在测试之后执行一个人工回滚,无论测试的实际结果如何。这样可以确保测试期间进行的所有更改都将回滚,测试后不会更改数据,但问题仍然存在?

你计划随着时间的推移测试模式更改吗?通常情况下,可以使用产品的新版本来更改模式。在这个领域,类似于Liquidbase或Flyway的工具很有用。例如,Flyway提供了"原子"迁移的概念,你基本上可以应用一系列"转换"模式以达到所需的状态。虽然它不属于测试工具的范畴,但如果愿意,你可以从Maven中运行此迁移工具。

正如你所见,这些问题没有"通常正确"的答案,不同的项目有不同的需求。因此,如果你想讨论一个你认为适用于你的产品的具体方法,欢迎提供更多的技术细节,但除此之外,我提到的所有框架/库都可以作为你询问的工具,但仍然缺少很多信息。

英文:

Let me present my opinion, hopefully it will clarify some things:

First of all, the data testing is really a broad topic, there is no one "silver bullet solution" and one adopted approach.

Usually, you can test your DAO code (SQL queries) with the help of a spring testing framework that, among other things provides a way to load some data before the test, run the code that interacts with the database, transaction management, etc.

This kind of test is not a unit testing, it falls into the category of an integration testing, which can be easily supported by maven/Gradle, be run from jenkins as a part of the pipeline.

There is also a tool called DbUnit that also works in this domain.

However, this is not the same as "testing the data".

Let me ask you a question, many questions actually, which all are boil down to: what exactly do you want to test? The fact that the data is consistent? Do you have constraints in the database, do you rely on them? Or avoid them and stuff like indices for performance reasons or any other reason that can be justified by your use case?

Do you want to populate the data during the test? How will it influence the data that already exists in production?

Do you plan to use transactions in the tests? For example spring testing framework that I've mentioned above is able to automatically open a "test transaction" before the test, and after the test execute an artificial rollback regardless the actual result of the test. This way it guarantees that all the changes done during the test will rollback and the data won't be changed after the test, but the question is open?

Do you plan to test the schema changes over-time. Usually, it's possible to change schema with new versions of the product. In this field the tools like liquidbase or flyway shine. For example, Flyway provides a notion of "atomic" migration and you basically can apply a series of migrations "transforming" the schema to the desired state. It doesn't fall into the category of testing tools though, although you can run this migration thing from maven if you wish.

As you see, these questions do not have "generally correct" answers, different projects have different needs. So bottom line if you want to discuss a specific approach that you think is applicable to your product - you'll welcome to provide more technical details, but other than that - all the frameworks/libraries that I mentioned can qualify as tools that you ask about, and still there is a lot of missing information here.

huangapple
  • 本文由 发表于 2020年7月31日 19:05:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63190700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定