TDD in python 3.1
I'm a C++ programmer who works via TDD. I am now learning python 3 and wish to continue with TDD. At the moment in C++, I give all my classes an interface and create mocked versions of them. I then pass pointers to these interfaces around in my code.
I'd like to know what libraries I should use in order to effectively tdd in python. What comes built in with python and what extras do i need. I have found this, and it seems very interesting:
http://www.voidspace.org.uk/python/mock/
Any alternatives worth also examining?
Any online resources, online tutorials or b开发者_运维问答ooks worth checking out?
Thanks!
I've never found the need for anything except that which python provides.
Because of Python's Duck typing, it is very, very easy to create Mock objects with a minimal implementation, and read out what is in them afterwards to check your assertions.
I find the unittest module works just fine for me too.
The mock library is widely used for mocking (although there are tons of other mock libraries as well, and often you don't need to mock at all). It's also common to use one of the testrunners, nose, pytest, zope.testrunner or the one in Distribute.
I've been looking at these lately myself, nose[1] promotes itself as a python tdd framework, mock, mox and most of the other mock libs for python don't seem to fit my mind. I'm not exactly sure mockers are required really. As mentioned before python's duck typing is very flexible, just make a base class and have it return the values you want/expect. But at the same time you'll have a lot of stubs. This mock lib, mocker[1], seems to make the most sense to me, but still not using it tbh.
In my recent experiences unittest (part of python's std lib) and stubs seem to fit the bill. I just draw up an interface that raise NotImplementedError for each method then extend that in my test interface (the mock mock object) that returns the expected/unexpected results and extend the interface to my classes I want to implement. I'm not sold on this solution either, I think a patch decorator might be nice for this. In python, interfaces are typically known as MixIn classes.
[1] http://somethingaboutorange.com/mrl/projects/nose/1.0.0/ [2] http://labix.org/mocker
精彩评论