Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you want opinions/experiences so I'm just giving my 2 cents.</p> <p>As you noticed there are a few Python testing tools/classes/frameworks, but most of the time given the simplicity/dynamism/openness of Python you will limit yourself to using ad-hoc relevant test cases which involve stubbing at the interface level, and a bit of unittest... until you start using the frameworks.</p> <p>There is nothing pejorative about monkey-patching, especially when it comes to performing testing/stubbing:</p> <pre><code>#!/usr/bin/env python # minimal example of library code class Class: """ a class """ def method(self, arg): """ a method that does real work """ print("pouet %s" % arg) #!/usr/bin/env python # minimal example for stub and tests, overriding/wrapping one method from Class import Class Class._real_method = Class.method def mymethod(self, arg): # do what you want print("called stub") # in case you want to call the real function... self._real_method(arg) Class.method = mymethod # ... e = Class() e.method("pouet") </code></pre> <p>Namespaces will allow you to patch stuff inside of imported modules inside of imported modules...</p> <p>Note that the above method does not work with classes in C modules. For them you can use a wrapper class that filters on class member names using getattr/setattr, and returns the redefined members from the wrapper class.</p> <pre><code>#!/usr/bin/env python # Stupid minimal example replacing the sys module # (not very useful / optimal, it's just an example of patching) import sys class SysWrap(): real = sys def __getattr__(self, attr): if attr == 'stderr': class StdErr(): def write(self, txt): print("[err: %s]" % txt) return StdErr() print("Getattr %s" % attr) return getattr(SysWrap.real, attr) sys = SysWrap() # use the real stdout sys.stdout.write("pouet") # use fake stderr sys.stderr.write("pouet") </code></pre> <p>Once you are becoming tired of performing ad-hoc testing, you'll find higher level stuff such as the ones you mentioned (stubble, fudge) useful, but to enjoy them and use them efficiently you have to first see the problems they solve and accept all the automatic stuff they do under the hood.</p> <p>It is probable that a part of ad-hoc monkey patching will remain, it's just easier to understand, and all the tools have some limitations.</p> <p>Tools empower you but you have to deeply understand them to use them efficiently.</p> <p>An important aspect when deciding whether to use a tool or not is that when you transmit a chunk of code, you transmit the whole environment (including testing tools). The next guy might not be as smart as you and skip the testing because your testing tool is too complex for him. Generally you want to avoid using a lot of dependencies in your software.</p> <p>In the end, I think that nobody will bother you if you just use unittest and ad-hoc tests/monkey-patching, provided your stuff works. Your code might not be <em>that</em> complex anyway.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload