Unit tests are meant to verify the functionality of isolated units of code. When dealing with code whose output depends on the system or system configuration, what are approaches to write effective unit tests? I feel this problem plagues lower level systems languages more so I am asking it here.
I solve this by writing "unit tests" that I then manually compare to the output of my terminal's utilities. It is the quickest way to verify units work as expected but it is obviously not automated.
Making a container or a VM to run integration tests seems like the next easiest way, not sure if there are other cost effective ways.
Say I have a function called
get_ip_by_ifname(const char *if_name, struct in_addr *ipaddr)
Inputs:
Returns:
The way I might test something like this works is write a test that logs each case's output to the terminal than run ip -c a
in another terminal and compare the info in the 2 outputs. I verify it works as expected manually with very minimal setup (just assigned multiple IP addresses to one of my interfaces).
I would like to test this in an automated fashion. Is there any way that wont be a time sink?