Articles tagged: testing

My First Patch

1861 days ago

This happened a little while ago but I didn’t get around to writing about it yet: I added a feature to py.test that is available as of release 2.4.0. :)

I have dabbled in dozens of open source projects, which might extend as far as filing bugs for a few handfuls. But I have rarely been motivated enough to dive in and figure out what was going on and add a new feature or fix a bug that was annoying me. I guess in the case of py.test I use it so heavily at work that I was “itchy” enough to really want to “scratch” it.

The problem – it is very easy to parametrize tests in py.test (feed different inputs into the same test), which is very useful for test isolation (ideally one assert statement per test) without heaps of repeated code. That’s all great, but there is no easy way to mix passing tests and xfail tests. Xfail means “expected to fail”, and this is a powerful way of writing “demonstration tests” for bugs that you are aware of but haven’t yet fixed. Yep, test driven development!

One way to do it could be to simply copy the test and have a version of it just for xfail cases. However if our test function’s contents are more complicated, this is obviously going to be bad repetition liable to fall out of date. With my patch you can now apply the marker directly to the tuple which has the parametrized values:

Incidentally you can apply any marker, not just xfail. At work we use marks to link tests to issues in our issue tracker (essentialy test metadata), and this would work here too.

As well as enjoying using py.test I like the dev community too. The founder Holger Krekel is undoubtedly a very clever guy (he founded and co-developed PyPy) and a good project leader, exactly what you would want in a BDFL. If you’re not using py.test for testing in Python – why not? :)

tags: ,

Comment

---

My talk at PyCon AU 2012 - "Funcargs and other fun with pytest"

2278 days ago

(What’s six months between friends…)

This last weekend I went down to Hobart for the third Australian PyCon conference. The first two were in Sydney, and the next one will also be in Hobart. I had a ball! I will hopefully revive this blog a bit more to write about other aspects of the conference but first up: the talk I gave, which was about the testing library pytest.

Pytest is a mature and comprehensive testing suite for Python projects, but it can be a little intimidating for newcomers. Where do these mysterious funcargs come from, how do parametrised tests work, and where are my xUnit-style setUp and tearDown methods?

Pytest lives by “convention over configuration” – which is great once you know what the conventions are. This talk will look at real examples of pytest in use, emphasising the features that differentiate it from nose.

Video:

I had fun picking out the comics – they are from comically vintage.

Slides, code.

tags: , , ,

Comment

---