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.4.tar.gz (431.6 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for numtest-2.0.4.tar.gz
Algorithm Hash digest
SHA256 2f344763d2769b0dbce74ca75a25a566764531d41a04b8b6a9abae69cb8c5c62
MD5 36b1e6008d729f9f5e36c71f18fb36f1
BLAKE2b-256 37e824aeac627b2e6968162dd160f9cdc2883399b5d40390af96e23aa5041a14

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