Live data from Hacker News

Running C unit tests with Pytest

p403n1x87.github.io

1–10 of 44 posts

Re: Running C unit tests with Pytest

#2
I doubt that it's pytest-specific, but the idea of using a unit test tool to explore a new problem space and build up to working product is powerful.

Maybe 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

#6
post #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 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

#8
post #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 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.

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

#9
post #8

Earlier 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?

Yes! Thank you. Fixed it.

Re: Running C unit tests with Pytest

#10
this seems cool, but... brittle? especially embedding build steps in python.

i'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.

Post reply on HN