From d0864783e361aab6578fe9a0fb5d98bab1bd811d Mon Sep 17 00:00:00 2001 From: Anthony Scopatz Date: Mon, 2 Apr 2012 22:22:20 -0500 Subject: [PATCH] Added mean tests. --- 5-Testing/Readme.md | 79 ++++++++++++++++++++++++++++++++++-------- 5-Testing/Readme.rst | 69 ++++++++++++++++++++++++++++-------- 5-Testing/mean.py | 10 ++++++ 5-Testing/test_mean.py | 28 +++++++++++++++ 4 files changed, 157 insertions(+), 29 deletions(-) create mode 100644 5-Testing/mean.py create mode 100644 5-Testing/test_mean.py diff --git a/5-Testing/Readme.md b/5-Testing/Readme.md index e485aa8..606626a 100644 --- a/5-Testing/Readme.md +++ b/5-Testing/Readme.md @@ -256,12 +256,30 @@ But, sometimes it's the case that your tests change the fixtures. If so, it's better for the setup() and teardown() functions to occur on either side of each test. In that case, the testing algorithm should be: -### Python Nose +```python +setup() +test1() +teardown() + +setup() +test2() +teardown() + +setup() +test3() +teardown() +``` + +* * * * * + +# Nose: A Python Testing Framework -The testing framework we’ll discuss today is called nose, and comes -packaged with the enthought python distribution that you’ve installed. +The testing framework we'll discuss today is called nose. However, there +are several other testing frameworks available in most language. Most +notably there is [JUnit](http://www.junit.org/) in Java which can +arguably attributed to inventing the testing framework. -**Where is a nose test?** +## Where do nose tests live? Nose tests are files that begin with Test-, Test\_, test-, or test\_. Specifically, these satisfy the testMatch regular expression @@ -271,26 +289,57 @@ can also create test functions which are not unittest.TestCase subclasses if they are named with the configured testMatch regular expression.) -Nose Test Syntax To write a nose test, we make assertions. +## Nose Test Syntax - assert (ShouldBeTrue()) - assert (not ShouldNotBeTrue()) +To write a nose test, we make assertions. -In addition to assertions, in many test frameworks, there are -expectations, etc. +```python +assert should_be_true() +assert not should_not_be_true() +``` -**Add a test to our work** +Additionally, nose itself defines number of assert functions which can +be used to test more specific aspects of the code base. -There are a few tests for the mean function that we listed in this +```python +from nose.tools import * + +assert_equal(a, b) +assert_almost_equal(a, b) +assert_true(a) +assert_false(a) +assert_raises(exception, func, *args, **kwargs) +assert_is_instance(a, b) +# and many more! +``` + +Moreover, numpy offers similar testing functions for arrays: + +```python +from numpy.testing import * + +assert_array_equal(a, b) +assert_array_almost_equal(a, b) +# etc. +``` + +## Exersize: Writing tests for mean() + +There are a few tests for the mean() function that we listed in this lesson. What are some tests that should fail? Add at least three test -cases to this set. +cases to this set. Edit the `test_mean.py` file which tests the mean() +function in `mean.py`. -*Hint: think about what form your input could take and what you should +*Hint:* Think about what form your input could take and what you should do to handle it. Also, think about the type of the elements in the list. What should be done if you pass a list of integers? What if you pass a -list of strings?* +list of strings? + +**Example**: + + nosetests test_mean.py -**Test Driven Development** +# Test Driven Development Some people develop code by writing the tests first. diff --git a/5-Testing/Readme.rst b/5-Testing/Readme.rst index a9ea7da..90216af 100644 --- a/5-Testing/Readme.rst +++ b/5-Testing/Readme.rst @@ -238,7 +238,7 @@ But, sometimes it's the case that your tests change the fixtures. If so, it's be for the setup() and teardown() functions to occur on either side of each test. In that case, the testing algorithm should be: -.. code-block:: +.. code-block:: python setup() test1() @@ -253,34 +253,75 @@ that case, the testing algorithm should be: teardown() ---------------------------------------------------------- -Python Nose ----------------------------------------------------------- -The testing framework we’ll discuss today is called nose, and comes packaged with the enthought python distribution that you’ve installed. +Nose: A Python Testing Framework +================================ +The testing framework we'll discuss today is called nose. However, there are several +other testing frameworks available in most language. Most notably there is `JUnit`_ +in Java which can arguably attributed to inventing the testing framework. -**Where is a nose test?** +.. _nose: http://readthedocs.org/docs/nose/en/latest/ +.. _JUnit: http://www.junit.org/ -Nose tests are files that begin with Test-, Test_, test-, or test_. Specifically, these satisfy the testMatch regular expression [Tt]est[-_]. (You can also teach nose to find tests by declaring them in the unittest.TestCase subclasses chat you create in your code. You can also create test functions which are not unittest.TestCase subclasses if they are named with the configured testMatch regular expression.) +Where do nose tests live? +************************* +Nose tests are files that begin with Test-, Test_, test-, or test_. +Specifically, these satisfy the testMatch regular expression [Tt]est[-_]. +(You can also teach nose to find tests by declaring them in the unittest.TestCase +subclasses chat you create in your code. You can also create test functions which +are not unittest.TestCase subclasses if they are named with the configured +testMatch regular expression.) Nose Test Syntax +**************** To write a nose test, we make assertions. -:: +.. code-block:: python + + assert should_be_true() + assert not should_not_be_true() + +Additionally, nose itself defines number of assert functions which can be used to +test more specific aspects of the code base. - assert (ShouldBeTrue()) - assert (not ShouldNotBeTrue()) +.. code-block:: python + + from nose.tools import * + + assert_equal(a, b) + assert_almost_equal(a, b) + assert_true(a) + assert_false(a) + assert_raises(exception, func, *args, **kwargs) + assert_is_instance(a, b) + # and many more! + +Moreover, numpy offers similar testing functions for arrays: + +.. code-block:: python + from numpy.testing import * -In addition to assertions, in many test frameworks, there are expectations, etc. + assert_array_equal(a, b) + assert_array_almost_equal(a, b) + # etc. -**Add a test to our work** +Exersize: Writing tests for mean() +********************************** +There are a few tests for the mean() function that we listed in this lesson. +What are some tests that should fail? Add at least three test cases to this set. +Edit the ``test_mean.py`` file which tests the mean() function in ``mean.py``. -There are a few tests for the mean function that we listed in this lesson. What are some tests that should fail? Add at least three test cases to this set. +*Hint:* Think about what form your input could take and what you should do to handle it. +Also, think about the type of the elements in the list. What should be done if you pass +a list of integers? What if you pass a list of strings? -*Hint: think about what form your input could take and what you should do to handle it. Also, think about the type of the elements in the list. What should be done if you pass a list of integers? What if you pass a list of strings?* +**Example**:: -**Test Driven Development** + nosetests test_mean.py +Test Driven Development +======================= Some people develop code by writing the tests first. If you write your tests comprehensively enough, the expected behaviors that you define in your tests will be the necessary and sufficient set of behaviors your code must perform. Thus, if you write the tests first and program until the tests pass, you will have written exactly enough code to perform the behavior your want and no more. Furthermore, you will have been forced to write your code in a modular enough way to make testing easy now. This will translate into easier testing well into the future. diff --git a/5-Testing/mean.py b/5-Testing/mean.py new file mode 100644 index 0000000..c04a0f1 --- /dev/null +++ b/5-Testing/mean.py @@ -0,0 +1,10 @@ +def mean(numlist): + try: + total = sum(numlist) + length = len(numlist) + except ValueError: + print "The number list was not a list of numbers." + except: + print "There was a problem evaluating the number list." + return total/length + diff --git a/5-Testing/test_mean.py b/5-Testing/test_mean.py new file mode 100644 index 0000000..69f4697 --- /dev/null +++ b/5-Testing/test_mean.py @@ -0,0 +1,28 @@ +from nose.tools import assert_equal, assert_almost_equal, assert_true, \ + assert_false, assert_raises, assert_is_instance + +from mean import mean + +def test_mean1(): + obs = mean([0, 0, 0, 0]) + exp = 0 + assert_equal(obs, exp) + + obs = mean([0, 200]) + exp = 100 + assert_equal(obs, exp) + + obs = mean([0, -200]) + exp = -100 + assert_equal(obs, exp) + + obs = mean([0]) + exp = 0 + assert_equal(obs, exp) + + +def test_floating_mean1(): + obs = mean([1, 2]) + exp = 1.5 + assert_equal(obs, exp) + -- 2.26.2