I am writing a parser and as a part of that, I have an Expander class that "expands" single complex statement into multiple simple statements. For example, it would expand this:
x = 2 + 3 * a into:
tmp1 = 3 * a x = 2 + tmp1 Now I'm thinking about how to test this class, specifically how to Arrange the tests. I could manually create the input syntax tree:
var input = new AssignStatement( new Variable("x"), new BinaryExpression( new Constant(2), BinaryOperator.Plus, new BinaryExpression(new Constant(3), BinaryOperator.Multiply, new Variable("a")))); Or I could write it as a string and parse it:
var input = new Parser().ParseStatement("x = 2 + 3 * a"); The second option is much simpler, shorter and readable. But it also introduces a dependency on Parser, which means that a bug in Parser could fail this test. So, the test would stop being a unit test of Expander, and I guess technically becomes an integration test of Parser and Expander.
My question is: is it okay to rely mostly (or completely) on this kind of integration test to test this Expander class?
Parsercould fail some other test is not a problem if you habitually commit only at zero failures, on the contrary it means that you have more coverage ofParser. What I would rather worry about is that a bug inParsercould make this test succeed when it should have failed. Unit tests are there to find bugs, after all—a test is broken when it doesn't but should have.