11

What is the best way to structure a python/cython project such that I can unit test code that resides in .pyx files? Is it possible to unit test this code in place or will refactoring allow me to achieve this in another way?

I am new to cython but have experience with Python TDD, mocking etc. I am also aware that the decision to cythonise some of the python code base could have been a premature optimisation decision. I have just joined a new team and therefore I am working on an existing code base. I am trying to get a feel for best practices.

Also in PyCharm, modules written in .pyx files are show as unresolved references in the editor - I have just read about pyximport but I am not sure if PyCharm is set up correctly for cython (I have the latest professional edition).

I am aware of the cython user guide but I cannot find advice on testing. I am also aware of pycharm's setup guide but this suggests that cython support is out of the box, which seems to contradict what I experience.

rnoodle
  • 534
  • 2
  • 5
  • 21
  • 3
    I resolved the PyCharm editor problem by adding .pyx to the recognised file types (I am not sure why this was missing). – rnoodle Sep 11 '15 at 10:38
  • Have you tried just adding your unit test code to the pyx file as if it was plain Python? Cython can handle the vast majority of plain Python code so there's a good chance it would just work (or only need tiny changes) and you wouldn't need to do anything special.... – DavidW Sep 14 '15 at 08:06
  • 1
    When I debug my tests, I cannot step into the pyx code. Is is possible to set up the debugger in PyCharm to achieve this? – rnoodle Oct 01 '15 at 09:46
  • I think that's expected. Have a look at http://docs.cython.org/src/userguide/debugging.html (but it's clearly a different mechanism to normal Python debugging). I'm afraid I know nothing about PyCharm. – DavidW Oct 01 '15 at 10:04
  • I believe this is an ongoing issue: it seems that debugging pyx files using the python debugger (pdb) is not possible yet, https://groups.google.com/forum/#!searchin/apam-python-users/pdb$20cython/apam-python-users/6rsRwcCAms4/Cw1IY1nvldYJ – rnoodle Oct 01 '15 at 10:08
  • @rnoodle: How do you "add .pyx to the recognised file types"? Do you just add the extension to a plain text file? – Hans Jan 06 '19 at 03:38

0 Answers0