3 ms·
This is pretty much my impression every time I try to test something in Python. I've never liked xunit, and I especially dislike it when it's a direct port fro
by jrockway 15y ago
This is pretty much my impression every time I try to test something in Python. I've never liked xunit, and I especially dislike it when it's a direct port from Java. Yes, it's familiar to Java programmers. But when I'm writing Python, I want something that looks like Python. (XML DOM libraries also like to think they're Java. That makes Common Lisp my favorite language for munging XML, since it's the only language bold enough to come up with some other API. But I digress...)
Also, I don't understand mocks in Python. Say I have a class Foo that has a method "bar" that I want to override. OK:
class FakeFoo(Foo):
def bar(self):
print "OH HAI"
Done. It's built in to the freakin' language :)
- kbd 15y agoWell, here's Michael Foord's (@voidspace, referenced in the slides) Mock library so you can see what extra stuff it gives you. http://www.voidspace.org.uk/python/mock/ http://www.voidspace.org.uk/python/mock/
- raymondh 15y agoFWIW, Michael Foord's mock library has been approved for inclusion in the Python's standard library. See http://www.python.org/dev/peps/pep-0417/ http://www.python.org/dev/peps/pep-0417/ and http://www.gossamer-threads.com/lists/python/dev/973274 http://www.gossamer-threads.com/lists/python/dev/973274
- tdavis 15y agoNow make some other code use your "mock" without editing its source. Now assert on the calls made to "bar()," including arguments. Now make it raise an exception without writing a second class. And make it return something else without writing a third. Now patch a built-in. And a context manager. And a dictionary. When you're done with that, mock a class without writing a new class at all. If you honestly think that sub-classing is equivalent to mocking/stubbing, you don't understand the purpose of the activity.
- jrockway 15y agoI know libraries can do all those things, but I've never preferred it over the real thing. As an example, I might do something like this (not Python): my $bar = 0; class FakeFoo { sub bar { return $bar } } my $thing = ThingToTest->new( foo => FakeFoo->new ); $foo = 42; is $thing->make_bar_two_bigger, 44; $foo = -10; is $thing->make_bar_two_bigger, -12; Mock enthusiasts would write: my $fake_foo = Mock->new( Foo->class )->instance; $fake_foo->return_sequence( bar => [42, -10] ); my $thing = ThingToTest->new( foo => $fake_foo ); is $fake_foo->make_bar_two_bigger, 44; is $fake_foo->make_bar_two_bigger, -12; ok $fake_foo->ensure_exhausted('bar'); Yes, it's less lines, but I think it's harder to read because the values are being set up long before they're used.
- jrockway 15y agoWell, this was typo-y. $foo in the first codeblock should be $bar, and $fake_foo->make_bar_two_bigger should be $thing->make_bar_two_bigger. More coffee is required :)
- pjscott 15y agoIf you want more pleasant test-writing in Python, nose is commonly used, high quality, and compatible with the standard library's unittest package: http://readthedocs.org/docs/nose/en/latest/ http://readthedocs.org/docs/nose/en/latest/