30.11.2014 Views

A4 portrait - PET: Python Entre Todos - Python Argentina

A4 portrait - PET: Python Entre Todos - Python Argentina

A4 portrait - PET: Python Entre Todos - Python Argentina

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

How should the test be? 18<br />

test_find_roots<br />

Given a list of coefficients and its roots, test that the result of the roots obtained<br />

from that coefficient matches the expected result. This test checks that<br />

find_roots works correctly. To do that, we iterate over a list of two tuples:<br />

• A tuple with the coefficients to call the function.<br />

• A tuple with the roots expected for those coefficients. These roots were<br />

calculated manually, not using the program.<br />

test_form_polynomial<br />

Given a list of roots, check that the coefficients are correct. This test checks that<br />

form_polynomial works correctly. In this case it’s a list of two tuples:<br />

• One with the roots<br />

• Th second with the coefficients expected for those roots.<br />

test_cant_find_roots<br />

Check that find_roots raises the right exception when real roots can’t be found.<br />

test_not_quadratic<br />

Checks what happens when the coefficients don’t belong to a quadratic<br />

function.<br />

test_integrity<br />

Given a set of roots, finds the coefficients, and for those coefficients find the<br />

roots again. The result of the roundtrip should be the same we started with.<br />

test_integrity_fails<br />

Check the case where integrity fails. In this case we use functions whose a<br />

value is not 1, so even if the roots are the same, it’s not the same quadratic<br />

function.<br />

4. At the end of the test we put this code:<br />

if __name__ == '__main__':<br />

unittest.main()<br />

That way, if we run python filename.py it will enter that if, and run all the tests<br />

in this file.<br />

Suppose we are in the folder where these files reside:<br />

pymag@localhost:/home/pymag$ ls<br />

polynomial.py test_polynomial.py test_polynomial_fail.py<br />

So, let’s run the passing tests:<br />

pymag@localhost:/home/pymag$ python test_polynomial.py<br />

......<br />

----------------------------------------------------------------------<br />

Ran 6 tests in 0.006s<br />

OK<br />

The output shows that all 6 tests in this class ran, and they all passed. Now we’ll see<br />

what happens when a test fails. For this, what I did was change one of the tests so<br />

that the second root is smaller than the first. I changed test_integrity so that one of<br />

the expected roots is (2, 1), when it should really be (1, 2) since the first root should<br />

be smaller. This is in test_polynomial_fail.py. So, if we run the tests that fail, we see:<br />

pymag@localhost:/home/pymag$ python test_polynomial_fail.py<br />

..F...<br />

======================================================================<br />

FAIL: test_integrity (test_polynomial.Testpolynomial)<br />

----------------------------------------------------------------------<br />

<strong>PET</strong>: English Translation (Issue 1, August 2010) — http://revista.python.org.ar

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!