Short Question
Does it fly in the face of the Pythonic view to reuse / repurpose python modules for projects out side of their intent?

Over the past several months I have been working on a project that uses the Python unittest module to run manual system tests. Please see questions 1 2 and 3 for greater detail on my project and some of the hurdles I have had to overcome.

I would like to know, in general, if it acceptable to reuse modules for other purposes? Most of the comments I have received have been along the lines of "this is not a unittest", or "you should write your own testrunner". My push back on that, is if a module does 90% of what you need it to do with litte to no modification and contains additional (knowingly unneeded) functionality why on earth would you write your own module?

  • 4
    Isn't it always good to use code outside it's original intent (provided it serves the function you want) in any language? I think it's sort of a fact of life that a module designer can never know all the uses their code might be put to. – Owen Aug 23 '11 at 4:14
  • What kind of "manual system tests?" – cdhowie Aug 23 '11 at 4:14
  • @cdhowie: Specifically running system tests on an embedded system. Each testcase actually calls a set of tests that sets power supplies, reads values back from oscilloscopes, dmms, etc... My linked questions go into more detail on the system under test – Adam Lewis Aug 23 '11 at 4:17
  • I think I agree with Owen on both counts. UNIX is still popular today, but most of its users aren't filing patent applications for AT&T. That said, it is possible to stretch software too far, if it isn't properly enhanced to deal with changing needs. – Matthew Flaschen Aug 23 '11 at 4:18
  • @Matthew F: What defines stretching software too far? The main issues I have ran into have all been based around convenience, meaning the core works great with no modification, changes are to make a user's life a bit easier. – Adam Lewis Aug 23 '11 at 4:24

Using the code for more than what the original authors intended is usually a good sign, in that the code provides useful tools. As far as re-purposing modules "outside of their intent" is concerned, well... this is obviously subjective and depends to what extent.

Using raw I/O functions in imaplib to implement some unrelated socket-based communication protocol could be considered bad style (to say the least). However, using the same imaplib library to implement some private variant of the IMAP protocol (unlikely to be the intended use) would be IMHO less of a problem.

It's totally understandable that you would find some convenient classes and methods in the unittest module to implement other types of tests. However, the problem is often one of code readability. To most Python developers, seeing the unittest module closely to the idiomatic recommended usage probably conveys the meaning of "this piece of code is a unit test". In terms of "self-documenting code", this is a total failure in the sens that it is misleading for other developers.

Ideally, this would be a non-question. The solution would be to refactor the unittest module so that code not specific to unit tests is moved to a test module or testbase module or some such thing. However, you might find a lot of resistance in such an effort, for political and practical reasons. It is, after all, a standard library module and can't be modified as easily as your own project.

Does it fly in the face of the Pythonic view to reuse / repurpose python modules for projects out side of their intent?

In general, I don't think this is "anti-Pythonic", although I think the fact that these comments have occured several times should mean something.

Maybe you can write some sort of systemtest module that imports the re-usable stuff from the unittest module. It's a temporary solution that gives both code clarity and allows you to re-use the code directly. Then, you can write a long comment in the systemtest module about how all the workarounds are actually better than writing your own test runner.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.