Running C unit tests with Pytest
p403n1x87.github.io
Running C unit tests with Pytest
1–10 of 44 posts
Re: Running C unit tests with Pytest
#2Maybe not so much in the UX space, but for ETL and back-end work, I'm a fan.
Re: Running C unit tests with Pytest
#3 @pytest.fixture
def libfact():
yield CDLL("./fact.so")
FWIW that's pretty confusing as it gives the impression the library is reloaded for every test, but iirc dlopen() will just return a handle to the existing one. I don't think ctypes has a good way to unload dlls so it should probably be `fixture(scope='session')`.That gets more relevant when on-the-fly compilation is added to the mix, spawning a compiler for every test is a complete waste of time.
Re: Running C unit tests with Pytest
#4https://github.com/rubenvannieuwpoort/c_unit_tests
Feels a bit more in line with the spirit of C (small language, few dependencies).
Re: Running C unit tests with Pytest
#5Re: Running C unit tests with Pytest
#6@pytest.fixture def libfact(): yield CDLL("./fact.so") FWIW that's pretty confusing as it gives the impression the library is reloaded for every test, but iirc dlopen() will just return a handle to the existing one. I don't think ctypes has a good way to unload dlls so it should probably be `fixture(scope='session')`. That gets more relevant when on-the-fly compilation is added to the mix, spawning a compiler for eve…
import _ctypes
import shutil
import tempfile
@pytest.fixture
def libfact():
tmp = tempfile.NamedTemporaryFile(delete=True)
shutil.copy2("./fact.so", tmp.name)
lib = CDLL(tmp.name)
yield lib
_ctypes.dlclose(lib.handle)
EDIT: fixed error mentioned in reply.Re: Running C unit tests with Pytest
#7How would you go to do this for c++? Specifically for classes and templates, is there an easy way to call code without having to manually write the demangled names of the functions?
Re: Running C unit tests with Pytest
#8@pytest.fixture def libfact(): yield CDLL("./fact.so") FWIW that's pretty confusing as it gives the impression the library is reloaded for every test, but iirc dlopen() will just return a handle to the existing one. I don't think ctypes has a good way to unload dlls so it should probably be `fixture(scope='session')`. That gets more relevant when on-the-fly compilation is added to the mix, spawning a compiler for eve…
You can use tempfiles and (on OSX) use the private API of ctypes to dlclose to close the handle. Different call on Windows I think. Something like import _ctypes import shutil import tempfile @pytest.fixture def libfact(): tmp = tempfile.NamedTemporaryFile(delete=True) shutil.copy2("./fact.so", tmp.name) lib = CDLL(tmp.name) yield lib _ctypes.dlclose(lib.handle) EDIT: fixed error mentioned in reply.
Re: Running C unit tests with Pytest
#9Earlier quoted context omitted.
You can use tempfiles and (on OSX) use the private API of ctypes to dlclose to close the handle. Different call on Windows I think. Something like import _ctypes import shutil import tempfile @pytest.fixture def libfact(): tmp = tempfile.NamedTemporaryFile(delete=True) shutil.copy2("./fact.so", tmp.name) lib = CDLL(tmp.name) yield lib _ctypes.dlclose(lib.handle) EDIT: fixed error mentioned in reply.
Did you mean to have the `tmp.name` as the library passed into `CDDL`? If not, what’s the purpose of the copy?
Re: Running C unit tests with Pytest
#10i've used check in the past and was happy enough with it, it didn't take long to learn. all unit test frameworks are basically the same at their core.
interop between c and python with ctypes is fun though.