Marty Wallace
Marty Wallace

Reputation: 35764

Unit testing - confused

I am learning unit testing and am getting confused by what a test is and the boundries of where to go with them.

So the answer in this question is very clear - https://stackoverflow.com/a/1257583/445330

The point that stands out to me is the likes of "It talks to the database".

In my particular system i am learning with, i need to test for particular nodes in a xml tree - represented as an object.

Aside from loading this object, how can I test for the presence of this value - or am i confusing unit testing and functional testing?

Upvotes: 1

Views: 180

Answers (3)

akuhn
akuhn

Reputation: 27803

The gist of the answer that you’re linking to is that

unit tests shall not depend on external resources.

so as long as your XML tree is programmatically created and stored in memory only rather than loaded from the filesystem (or any other external resource) you’re doing it right.

Programmatically creating XML trees can be awkward, so what you can do is to store the tree as an XML file in the same package as your tests and then load them with

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

which does not count as an external resource because it is loaded from the classpath of your test suite.

Upvotes: 2

user1594895
user1594895

Reputation: 587

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

Upvotes: 0

kldavis4
kldavis4

Reputation: 2197

If this is just a unit test, you are testing the code that is reading the xml tree and checking the tree for whatever you are expecting. You could load a test object in a number ways. One would be to have the unit test construct the object as a part of the setup for the test. Another would be to store the test version of the xml tree in a flat file (alongside the test classes) and have the test parse the file at test time.

The point is that the input to your test needs to be a control, not something that depends on the state of some external resource (like a database).

Upvotes: 0

Related Questions