Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 155 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

Seite - 155 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Bild der Seite - 155 -

Bild der Seite - 155 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 155 -

6.6 TestingCode 155 In [6]: rel_diff Out[6]: -1.9073486327552798e-16 Clearly,diffhere would certainly not be smaller than a tolerance of 1E−15 (as we used above). So, to check whethera andb are equal, we should rather proceed as In [7]: tol = 1E-15 In [8]: rel_diff < tol Out[8]: True 6.6.4 ConstructingUnitTestsandWritingTestFunctions Python has several frameworks for automatic testing of software. By just one command, you can get Python to run through a (potentially) very large number of tests for various parts of your software. This is an extremely useful feature during programdevelopment:wheneveryouhavedonesomechanges tooneormorefiles, launch the test commandandmakesurenothingisbrokenbecauseofyouredits. Thetest frameworksnoseandpy.testareparticularlyattractive,since theyare very easy to use. Tests are placed in special test functions that the frameworks can recognizeand runforyou.The requirements toa test functionare simple: • thenamemust start withtest_ • the test functioncannothaveanyarguments • the tests inside test functionsmust bebooleanexpressions • abooleanexpressionbmustbe tested inanassert statementof the formassert b, msg. Whenb is true, nothing happens. However, ifb is false,msg is written out,wheremsg is anoptionalobject (stringornumber). Supposewehavewrittena function def add(a, b): return a + b Acorrespondingtest functioncan thenbe def test_add(): expected = 2 computed = add(1, 1) assert computed == expected, ’1+1={:g}’.format(computed) Test functions can be in any program file or in separate files, typically with names starting with test. You can also collect tests in subdirectories: runningpy.test -s -vwill actually run all tests in all test*.pyfiles in all subdirectories, while nosetests -s -v restricts the attention to subdirectories whose names start with testorendwith_testor_tests. As long as we add integers, the equality test in the test_add function is appropriate,but ifwe try tocalladd(0.1, 0.2) instead,wewill face therounding
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python 3.6
Band
Second Edition
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2020
Sprache
englisch
Lizenz
CC BY 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
356
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python