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

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for numtest-2.0.3.tar.gz
Algorithm Hash digest
SHA256 df3b37b663d4bf6266f783e9b0c3b07ff6eda027f7535f8516935357887281c1
MD5 400bdf448a0ce1339156fb761602e733
BLAKE2b-256 6cbf833dd70d5f62d5c65e321ec51ac4d862a961a431cf1922442ac8bb26b864

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