Answer the question
In order to leave comments, you need to log in
Can a unit test of a class method also depend on other methods?
In short, the whole question is in the subject.
In more detail, I'm new to testing, writing the first tests in my life (using the Google Test Framework) for a doubly linked list class.
We need to test, say, the pop_back method (remove the last element of the list). But if you do not use other class methods, then in order to somehow fill this list, you will have to write low-level code for creating nodes, linking them, etc. in the test. How to be?
Answer the question
In order to leave comments, you need to log in
One test, one problem. The test must identify STRICTLY one problem! As a conclusion: you should not make one test depend on another.
I don't think you understand what a unit test is. Here is its fairly correct definition: Unit Test - Definition
Yes maybe. Everything I write here is my IMHO, due to the eternal lack of time and errors in basic functions (high-level code is extremely difficult to test and I don’t know if there are standard test automation methods).
Unnecessarily complex code of test functions also needs to be tested - so there is nothing wrong with using functions that have been verified by other tests. Native low-level functions are worth using if they are either simpler or put the object into some controlled state that is difficult to achieve with a public interface. So the test code should be something like this.
1. Collect the list we need.
2. Check that the list is in the state we need.
3. Remove one element.
4. Verify that the new state is correct.
There can be many checks in paragraphs 2 and 4 - for example, in paragraph 2 "the list is correctly linked, it has 3 elements and the last two B, C", in paragraph 4 - "the list is correctly linked, it has 2 elements and the last B". The main thing is that a) one concept is tested - for example, "removal from the end works"; and b) all the concepts that are needed for the test to work correctly - for example, "an empty list is constructed", "add to an empty" and "add to the end" - also need to be tested.
With the dependence of the tests on each other a little ambiguous. Tests should not physically interfere with each other: when debugging, you need to run some subset or even one failed test. Or the internal structure of the object has changed and we remove part of the tests altogether, since the tested concept disappears. And the logical one - "if the addition does not work, this test is meaningless" - yes, no problem!
Opinions differ. Does this mean that there is no definite answer to my question?
Well, in general, according to the definition of unit testing, one test tests one method. If the test requires some input, then you give it a blank. So the test script would be more like this:
1. Create a (hardcoded) list.
2. Remove the element (using the method under test).
3. Check that the element has been removed.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question