From 1274b14f676c50a7e578a0c7376dd0d047407249 Mon Sep 17 00:00:00 2001 From: Mike Jackson Date: Wed, 3 Apr 2013 02:45:30 -0700 Subject: [PATCH] Add assert_raises example --- testing/Writing.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/testing/Writing.md b/testing/Writing.md index e826ce4..c62098e 100755 --- a/testing/Writing.md +++ b/testing/Writing.md @@ -174,7 +174,14 @@ The latter requires us to check whether an exception was raised which we can do except ValueError: assert True -This is like catching a runtime error. If an exception is raised then our test passes (`assert True`), else if no exception is raised, it fails. +This is like catching a runtime error. If an exception is raised then our test passes (`assert True`), else if no exception is raised, it fails. Alternatively, we can use `assert_raises` from `nose`, + + from nose.tools import assert_raises + + def test_123(): + assert_raises(ValueError, calculate_weight, 123) + +The assert fails if the named exception is *not* raised. One other test we could do is `calculate_weight('GATCX')` for which we can add another runtime test, -- 2.26.2