How to properly test C API from python, the C API returns return codes as errors

Viewed 1603

My Setup

I am testing functions in a C library using pytest and ctypes from Python. Each function in the C library calls a function on an embedded linux PCI board, the C library functions then return an integer which maps to a set of return codes. If the function is successful it returns 0, otherwise it returns some other error code.

Problem

I am looking for some insight as to the best way to test this setup, essentially I am trying to figure out the best practice for testing this library. The best way to throw exceptions and assert test cases. The testing environment I am using currently uses pytest, it would be a pain to switch.

There is a lot of state that needs to be setup for tests in order to test all the different conditions because the embedded linux board has state. So ideally python will catch all of the errors that occur in a test if there is an error.

My Current Setup

Currently I have python functions wrapping the C library code. A test will call the python code, if the function doesn't return 0 then the wrapping python function will throw an exception. Because I am using ctypes to call function on the DLL, all of the C functions are wrapped anyways by python to make them easier to call so passing arguments is easier.

Advantages

  • If there is a problem in the setup of a test or any intermediate test then an exception will be thrown. This makes it easier to debug failed test cases when the setup of the test case fails.

Disadvantages

  • Asserting that a function is successful (returns 0) is useless because an error will be thrown before the function returns.
  • If you are testing that calling a function under certain conditions causes an error you have to wrap the test case messy try-except block

    try:
       return_code = call_to_c_api()
       assert return_code == 0, "Message about test case"
    except MyCustomException:
       assert MyCustomException.message="Return code of python wrapped api"
    

Another Option

Do not wrap the C library with a python library and make it up to the tests to assert failure if one of the setup functions doesn't work.

Advantages

  • When you expect a failure to occur it is very easy to just assert that the return value from the C API is not O

Disadvantages

  • In order to have tests that are easy to debug, every single call to the C API will need code in the python test to make sure it passed. Even if it is just a call for the setup of the test. This means almost double the lines of code for every test. (Or you could not catch errors in the setup and only assert when a test fails. This means painful debugging when a test case's setup fails)

If anyone could give any insight into the best way to test this it would be much appreciated! There are probably other ways to test this that I have not yet considered!

Cheers!

0 Answers
Related