Skip to content

Instantly share code, notes, and snippets.

@showell
Created October 10, 2012 04:20
Show Gist options
  • Save showell/3863138 to your computer and use it in GitHub Desktop.
Save showell/3863138 to your computer and use it in GitHub Desktop.
xunit is cruft in python
TESTS = []
def suite():
for test in TESTS:
print 'running %s' % test.__name__
test()
def test(f):
TESTS.append(f)
return f
@test
def basic_arithmetic():
assert 1 + 1 == 2
@test
def basic_array_stuff():
assert 1 in [1, 2]
suite()
@rking
Copy link

rking commented Oct 10, 2012

Hey showell!

Though this is mostly true, there are still some niceties that you'll have to
reproduce, one by one. Look at the badness of the output of:

def x():
  1 + 2
@test
def basic_arithmetic():
  assert x() == 2

Shows this:

running basic_arithmetic
Traceback (most recent call last):
  File "/home/rking/shared/src/scrap/foo.py", line 24, in <module>
    suite()
  File "/home/rking/shared/src/scrap/foo.py", line 8, in suite
    test()
  File "/home/rking/shared/src/scrap/foo.py", line 18, in basic_arithmetic
    assert x() == 2
AssertionError

Notice how there's nothing about 3 anywhere in there. ☹

@showell
Copy link
Author

showell commented Oct 10, 2012

Yup, of course, but Python's bare assert already gives you the line number, which gets you 99% there. And assert_equals is trivial to write. The one thing that's slightly painful to reinvent is assert_raises. The point of this gist isn't necessarily to argue against xunit; it's more to show that the fundamental pieces of xunit are dirt simple to simulate without the framework. (This would be less true in Java, I imagine.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment