Skip to main content

Numeric Doctest

Project description

Numtest is a doctest extension that simplifies the test of numerical results.

It provides a new doctest directive NUMBER to use with numerical tests:

>>> import math
>>> math.pi # doctest: +NUMBER
3.14

To enable it, import the module numtest before you run the tests.

How does it work ?

A major issue in numerical tests is the control of the precision of the results.

Numtest infers the precision that you want from the number of digits used in the expected result: the string "3.14" used in the above example means that the best three-digit approximation of math.pi should be 3.14.

For example, a math module that would declare pi as any of the numbers

3.141592653589793, 3.14, 3

would pass the above test while the values

100, 3, 3.1, 3.149

would fail the same test.

Additional Features

Numtest supports comparison of list of numbers and NumPy arrays. For example, the test below passes:

>>> import numpy
>>> x = numpy.linspace(0.0, 1.0, 4)
>>> x # doctest: +NUMBER
[0.00, 0.333, 0.667, 1.00]

Only the shape and values of lists or arrays are tested: lists may be successfully compared to arrays, arrays of integers with arrays of floats, etc.

Gotchas

Be aware that NumPy displays arrays with only a 8-digits precision by default. Hence the test below would fail:

>>> x # doctest: +NUMBER
[0.00, 0.333333333333, 0.667, 1.00]

However, the display precision used by NumPy is configurable. The prior invocation of

>>> numpy.set_printoptions(precision=17)

will make the test pass as expected.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

numtest-2.0.5.tar.gz (431.5 kB view details)

Uploaded Source

File details

Details for the file numtest-2.0.5.tar.gz.

File metadata

  • Download URL: numtest-2.0.5.tar.gz
  • Upload date:
  • Size: 431.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for numtest-2.0.5.tar.gz
Algorithm Hash digest
SHA256 94a810d2424f69e2884c2451811782b015de03611b659a9a389ffa80492b47a5
MD5 254a2e419bc3742fa8ae43690cde4115
BLAKE2b-256 5023db45a9e7b1339ada6b4f160c4021407621483c6e732fddc3865acd9de485

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page