1

Let's say I'm testing my code and I have designed 5 functions to to 5 different things. The data function_a() returns is passed to function_b()... ect all the way to function_e(). See below.

var1 = function_a()
var2 = function_b(var1)
...
var5 = function_e(var4)

So all of these functions are fairly interdependent and reliant on the data from the previous function.

How do I test this properly? Five assertions in a single test seems like a code smell to me, but according to the accepted answer here passing data between separate tests (which would be necessary here I think) is a bit of a code smell.

Community
  • 1
  • 1

2 Answers2

2

The fact the the data is passed from one function to the next is irrelevant from a testing point of view. When testing function_a you'll just be asserting that it returns the expected value (presumably based on some state). You'll therefore know what the expected return value is.

When testing function_b on its own you can just pass it the value you expected to be returned from function_a and validate it returns the expected value. you can also test what happens when you pass it invalid values (which would be difficult when testing with the actual return value of function_a.

The same applies to the other functions. You should split your tests up and test in isolation. The fact that the are called sequentially is of no consequence to each function individually. they don't know where their inputs came from

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
1

One assertion per test is just a recommendation (pretty reasonable though) and is related to testing one thing at a time in each of your unit tests. Here, you have multiple functions - multiple components of a bigger "system".

Ideally, you need to test each of these functions in an isolation and have a set of integration tests checking how would it work if you go through the complete chain of functions.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195