My setting
I am testing functions in the C library using pytest and ctypes from Python. Each function in C library calls a function on the built-in PCI PCI card, C library functions then return an integer that maps to a set of return codes. If the function succeeds, it returns 0; otherwise, another error code is returned.
Problem
I am looking for some idea of ββhow to best test this setting, in fact, I am trying to find the best practice for testing this library. The best way to throw exceptions and approve test cases. In the test environment I'm using, pytest is currently being used, it would be painful to switch.
There are many states that you need to configure for testing in order to test all the different conditions, since the built-in Linux board has state. Therefore, ideally, python will catch all errors that occur in the test, if there is an error.
My current setup
I currently have python functions that carry the C library code. The test will call python code, if the function does not return 0, then the python function to wrap will throw an exception. Since I use ctypes to call a function in a DLL, all C functions are wrapped up anyway with python to make calling easier in a way that makes passing arguments easier.
Benefits
- If there is a problem setting up a test or any intermediate test, an exception will be thrown. This makes it easier to debug failed test cases when the test case installation fails.
disadvantages
- The statement that the function is successful (returns 0) is useless because an error will be thrown before the function returns.
If you test that calling a function under certain conditions causes an error, you should wrap the messy try-except block test file
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 variant
Do not terminate the C library with the python library and do not run tests to confirm failure if one of the configuration functions does not work.
Benefits
- When you expect failure, itβs very simple to assert that the return value from the C API is not O
disadvantages
- In order for tests that are easy to debug for, each C API call will require code in a python test to make sure it passes. Even if this is just a call to set up a test. This means that for each test there are almost twice as many lines of code. (Or you couldnβt catch the configuration errors and only say when the test fails. This means painful debugging if the test case fails to install)
If anyone could figure out how best to test this, it would be very appreciated! There are probably other ways to verify this, which I have not yet reviewed!
Hooray!
source share