0
Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

# Why you should use a testing framework

1,865

Published on

The case for using a testing framework to make your software robust, with a simple example in MATLAB.

The case for using a testing framework to make your software robust, with a simple example in MATLAB.

Published in: Technology
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
1,865
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
12
0
Likes
0
Embeds 0
No embeds

No notes for slide
• In this short presentation, I&apos;m going to try and persuade you that you should be using a testing framework for any software that you write. The example we&apos;ll look at uses MATLAB, but the argument is applicable to any software development.
• Hopefully you&apos;ll be looking at this problem and thinking “this is easy, I learnt Pythagoras&apos;s theorem when I was in school”. That&apos;s good. In that case our first attempt at solving the problem will look familiar to you.
• As Pythagoras said, “the square of the longest side is equal to the sum of the squares of the other two sides”, or whatever the Greek equivalent is. This looks like we might have cracked it straight away. Let&apos;s test it and find out.
• First we test the function under normal conditions. As every school-kid knows, for a right-angled triangle with sides of length 3 and 4, the hypotenuse squared is 3 squared plus 4 squared, which is 25 and so the hypotenuse is of length 5. Likewise, the hypotenuse of the unit square is root 2. So far, so good.
• What if we don&apos;t pass the function any arguments? This test shows that an error is thrown when no arguments are passed to the function. You may decide that that is the most appropriate behaviour. On this occasion, let&apos;s decide that we want default values.
• Here&apos;s our second attempt. The additional code sets a default value of 1 when no input was passed for that length.
• Now that we&apos;ve changed the function, we need to check that our new code passes the test. Happily, in this case it does.
• Another test is to see what happens when you pass empty inputs in to the function. Empty inputs should probably work in the same way as no inputs, so we have a problem here.
• Here&apos;s our third attempt. We need to test that this version works with empty inputs but we also need to check that we haven&apos;t introduced new bugs. Running your old tests to make sure that there are no new bugs is called regression testing.
• Here is our set of tests again. As you can imagine, typing in all these tests every time you change something is already really time consuming, and we&apos;re not done yet.
• If you&apos;ve done any numeric programming before, you&apos;ll have realised that bad things often happen when you introduce very big or very small inputs. In this case, squaring the numbers causes them to over or underflow.
• This fourth attempt is the version that most modern scientific software uses. Explaining how it works is beyond the scope of this presentation. The important thing is that we&apos;ve changed the function, so we need to run all the tests again.
• I&apos;m now really bored of typing in tests, and yet we still haven&apos;t exhausted the test possibilities, e.g., What happens when non-numeric arguments are passed? What happens if non-scalar arguments are passed? What happens in the 3D (or higher dimension) case?
• So we&apos;ve established two things. That we need to write lots of tests, and that we need to run each test lots of times. In order to save our sanity, we need a way of easily creating and running and maintaining these tests. Otherwise, laziness will take over and we&apos;ll just not bother.
• We could write our own function to run the tests. This is much better, but notice that there&apos;s a lot of effort involved in displaying the results, when we really just want to think about the tests themselves.
• A testing framework is just a formalised version of the contents of the previous slide. They generally consist of: Lots of short functions or methods representing individual tests
• A suite function that runs some or all of the tests.
• And some utility functions for handling errors and displaying results.
• Here&apos;s an example using the MATLAB xUnit framework. To use it, first, we create some test functions, and place them in the same directory. Here you can see the five tests we thought of earlier.
• We just need two more lines of code. The first line creates a suite of tests from each function, and the second line runs them all, telling us that we passed them.
• If we run the tests on our original version of the function instead, then the test suite shows us where our problems lie.
• To summarise: Firstly, thorough testing is necessary, even when you think the answer should be easy. In this case, the overflow bug was quite obscure, and you might not have picked up on it without testing.
• Secondly, if we use a testing framework, then thorough testing needn&apos;t be onerous. Each test was a one line function, creating and running the suite were one line each.
• These two ideas taken together imply that testing frameworks are a great idea.
• If you want to find out more, then check out these resources. Thanks for listening.
• ### Transcript

• 1. <ul><li>Why you should use a testing framework </li></ul><ul>by Richard Cotton </ul>
• 2. Our goal: write a function to calculate the length of the hypotenuse of a right-angled triangle from the other two sides
• 3. &nbsp;
• 4. &nbsp;
• 5. &nbsp;
• 6. <ul>( nargin returns the number of arguments passed into the function) </ul>
• 7. &nbsp;
• 8. <ul>In MATLAB, [] represents an empty matrix. </ul>
• 9. &nbsp;
• 10. &nbsp;
• 11. <ul>Uh-oh. The answers should be 1.414e300 and 1.414e-300 respectively. </ul>
• 12. &nbsp;
• 13. &nbsp;
• 14. <ul>The story so far </ul><ul><li>We need to do lots of tests.
• 15. We need to do each test lots of times. </li></ul><ul>This means that ... </ul><ul><li>It would be a good idea to have a suite of tests that can be easily created/run/maintained. </li></ul>
• 16. &nbsp;
• 17. <ul><li>Lots of short functions/methods representing individual tests </li></ul>
• 18. <ul><li>Lots of short functions/methods representing individual tests
• 19. A suite function that runs some or all of the tests. </li></ul>
• 20. <ul><li>Lots of short functions/methods representing individual tests
• 21. A suite function that runs some or all of the tests.
• 22. Some utility functions for handling errors and displaying results. </li></ul>
• 23. &nbsp;
• 24. &nbsp;
• 25. &nbsp;
• 26. <ul><li>Thorough testing is necessary. </li></ul>
• 27. <ul><li>Thorough testing is necessary.
• 28. If we use a testing framework, then thorough testing needn’t be onerous. </li></ul>
• 29. <ul><li>Thorough testing is necessary.
• 30. If we use a testing framework, then thorough testing needn’t be onerous. </li></ul><ul>1 &amp; 2 =&gt; Testing frameworks are a great idea. </ul>
• 31. <ul>Resources </ul><ul>Lists of testing frameworks </ul><ul>http://c2.com/cgi/wiki?TestingFramework http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks </ul><ul>Testing Strategies </ul><ul>&amp;quot; Testing Computer Software &amp;quot; by Kaner et al, especially Ch. 3 http://en.wikipedia.org/wiki/Software_testing http://software-carpentry.org/4_0/test/ </ul>