1

我正在学习单元测试,并且对测试是什么以及使用它们的边界感到困惑。

所以这个问题的答案很清楚 - https://stackoverflow.com/a/1257583/445330

对我来说突出的一点是“它与数据库对话”之类的。

在我正在学习的特定系统中,我需要测试 xml 树中的特定节点 - 表示为一个对象。

除了加载这个对象,我如何测试这个值的存在——或者我混淆了单元测试和功能测试?

4

3 回答 3

2

您链接到的答案的要点是

单元测试不应依赖于外部资源。

因此,只要您的 XML 树是以编程方式创建并仅存储在内存中,而不是从文件系统(或任何其他外部资源)加载,那么您就做对了。

以编程方式创建 XML 树可能很麻烦,因此您可以将树作为 XML 文件存储在与测试相同的包中,然后使用

this.getClass().getClassLoader().getResourcesAsStream(name);

这不算作外部资源,因为它是从测试套件的类路径加载的。

于 2013-01-04T20:46:51.257 回答
0

In my test I made not only one but more and more tests on a particula function, so Unit Test is a part of the possibilities. I select specific case for example in a division i use 0 for both operand, single operand etc, limit of the specific numerical interval, but also normal situation. All test are predictable, so when i change the code the execution of case assure the stability of change made.

In your case i think you are doing a single unit testing.

I accept the wikipedia definition of unit testing and functional testing.

Functional testing is wide and is program oriented. Unit testing is oriented to the minimal part of testable code and should be as much generic as possible.

In your specific case "In my particular system i am learning with, i need to test for particular nodes in a xml tree - represented as an object" if the result of function is the object you should have a well known input string, file , object static defined, and an assertion that the output for specific input is an object that has the particular node, or better extract the return object, extract the particular node and assert that the particular node should be equal to a particular value /object

I hope to be useful

于 2013-01-04T20:30:19.770 回答
0

如果这只是一个单元测试,那么您正在测试读取 xml 树的代码并检查该树是否符合您的期望。您可以通过多种方式加载测试对象。一种方法是让单元测试将对象构造为测试设置的一部分。另一种方法是将 xml 树的测试版本存储在一个平面文件中(与测试类一起),并让测试在测试时解析文件。

关键是测试的输入需要是一个控件,而不是依赖于某些外部资源(如数据库)的状态的东西。

于 2013-01-04T20:22:38.053 回答