junit:开发测试用例的最佳实践?
技术:Junit最新版本 应用程序是面向业务的。
有些人使用硬编码数据进行测试用例,有些人使用属性文件,有些人使用 xml 文件。据我所知,xml 比其他两个更好。有没有更好的方法在工业中使用。请建议开发测试用例的最佳实践。
Technology: Junit latest version
Application is business oriented
Some people use hard coded data for test case some use properties files and some xml files. As per my knowledge xml is better than other two. Is there some better approach in use in industry. Please suggest best practice to develop test cases.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
测试中的数据表示和传递给正在测试的函数的数据之间的映射尽可能透明,这一点很重要。如果数据很少并且易于在源中观察,那么硬编码数据是完全可以的。为了理解测试用例而需要打开的窗口越少越好。
XML 最适合嵌套的树状数据,但它有点冗长。 YAML 也可能对此有好处。对于平面数据,属性和仅行组织的文件都可以。
没有单一格式在各方面都优于所有其他格式。为特定的测试套件/主题区域选择最简单和最自然的。当您需要快速生成越来越多的测试用例时,以及当您调查回归时,投入一些精力来处理最自然的格式确实是值得的。例如,在我们的项目(相当大)中,我们必须发明几种数据表示形式并编写(简单的)自定义解析器,以便使测试用例的数据写入和读取变得轻而易举。
It is important that the mapping between data representation in the test and data passed to the function being tested is as transparent as possible. Hard-coded data is totally ok if the data are few and easy to observe right in the source. The fewer windows you need to keep open to understand a test case, the better.
XML is best for nested, tree-like data, but it's a bit wordy. YAML may also be good for this. For flat data, properties and just line-organized files are ok.
There's no single format which is superior to all others in all ways. Choose the easiest and most natural for a particular test suite / subject area. Investing some effort in handling the most natural format does pay when you need to quickly produce more and more test cases, and then again when you investigate a regression. E.g in our project (quite large) we had to invent several data representations and write (simple) custom parsers just to make writing and reading data for test cases a breeze.
我认为没有最佳实践。我建议您使用最适合您的特定问题空间以及您需要执行的测试类型的测试。如果您需要编码的测试本质上涉及调用具有大量不同输入的方法,那么数据驱动方法(使用属性文件、XML 或其他内容)是一个好主意。如果不是,那就是个坏主意。
需要注意的一件事是花费太多时间创建复杂的基础设施,以便您可以整齐地编写测试代码。
I don't think there are best practices. I suggest that you use the one that works best for your particular problem space, and the kind of testing you need to perform. If the tests you need to code essentially involve calling methods with a large number of different inputs, then a data driven approach (using properties files, XML, or something else) is a good idea. If not, it is a bad idea.
The one thing to watch is spending too much time creating complicated infrastructure so that you can code your tests neatly.
我会尽力保持测试快速而简单。测试运行得越快,您可以添加到构建中的测试就越多。
xml 的缺点:解析非常昂贵,还要从 DOM 中读取值。对于表格数据,我会使用某种 CSV 格式的平面文件。对于键/值数据,一个简单的属性文件绝对足够了。
使用 JUnit,我们处于单元测试级别,我们想知道公共接口是否根据规范实现,以及它们是否以定义的方式针对所有可能的输入运行。因此,我通常在测试方法中对测试值进行硬编码,因为它们通常不会更改(无需在测试类之外编辑值)
I'd try to keep the tests fast and simple. The faster, the tests run, the more tests you can add to your build.
The disadvantage of xml: parsing is quite expensive, reading the values from the DOM too. For tabular data, I'd use flat files in some sort of CSV format. For key/value data, a simple properties file is absolut sufficient.
With JUnit, we're on unit testing level, we want to know if the public interface are implemented according to the specs and if they behave in a defined way for all possible input. Therefore I usually hardcode the test values in the test methods, because they usually don't change (no need to edit the values outside of the test classes)
只要您的数据文件位于类路径中,我就没有看到问题。单元测试最佳实践之一是测试用例应该隔离并且不依赖于外部资源。如果您使用外部文件(xml、csv 等),您肯定会影响您的测试速度。
如果数据量不大,使用参数化测试可能是一个不错的选择。是的,它是硬编码的,但以一种干净的方式:
本教程关于单元测试最佳实践 讨论单元测试隔离和其他最佳实践。
As long as your data files are in the classpath I don't see a problem. One of the unit testing best practices is that a test case should be isolated and not depend on external resources. If you use external files ( xml, csv, etc ) you will affect your test speed for sure.
If the amount of data is not big, Using parameterized tests can be a good fit. Yes it is hardcoded but in a clean way:
This tutorial about Unit testing best practices talks about unit tests isolation and other best practices.