1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
from __future__ import annotations
import contextlib
import doctest
import math
import os
import pathlib
import unittest
import warnings
from pint import UnitRegistry
from pint.testsuite.helpers import PintOutputChecker
class QuantityTestCase:
kwargs = {}
@classmethod
def setup_class(cls):
cls.ureg = UnitRegistry(**cls.kwargs)
cls.Q_ = cls.ureg.Quantity
cls.U_ = cls.ureg.Unit
@classmethod
def teardown_class(cls):
cls.ureg = None
cls.Q_ = None
cls.U_ = None
@contextlib.contextmanager
def assert_no_warnings():
with warnings.catch_warnings():
warnings.simplefilter("error")
yield
def testsuite():
"""A testsuite that has all the pint tests."""
suite = unittest.TestLoader().discover(os.path.dirname(__file__))
from pint.compat import HAS_NUMPY, HAS_UNCERTAINTIES
# TESTING THE DOCUMENTATION requires pyyaml, serialize, numpy and uncertainties
if HAS_NUMPY and HAS_UNCERTAINTIES:
with contextlib.suppress(ImportError):
import serialize # noqa: F401
import yaml # noqa: F401
add_docs(suite)
return suite
def main():
"""Runs the testsuite as command line application."""
try:
unittest.main()
except Exception as e:
print("Error: %s" % e)
def run():
"""Run all tests.
:return: a :class:`unittest.TestResult` object
Parameters
----------
Returns
-------
"""
test_runner = unittest.TextTestRunner()
return test_runner.run(testsuite())
_GLOBS = {
"wrapping.rst": {
"pendulum_period": lambda length: 2 * math.pi * math.sqrt(length / 9.806650),
"pendulum_period2": lambda length, swing_amplitude: 1.0,
"pendulum_period_maxspeed": lambda length, swing_amplitude: (1.0, 2.0),
"pendulum_period_error": lambda length: (1.0, False),
}
}
def add_docs(suite):
"""Add docs to suite
Parameters
----------
suite :
Returns
-------
"""
docpath = os.path.join(os.path.dirname(__file__), "..", "..", "docs")
docpath = os.path.abspath(docpath)
if pathlib.Path(docpath).exists():
checker = PintOutputChecker()
for name in (name for name in os.listdir(docpath) if name.endswith(".rst")):
file = os.path.join(docpath, name)
suite.addTest(
doctest.DocFileSuite(
file,
module_relative=False,
checker=checker,
globs=_GLOBS.get(name, None),
)
)
@unittest.skip("Not working on Debian")
def test_docs():
suite = unittest.TestSuite()
add_docs(suite)
runner = unittest.TextTestRunner()
return runner.run(suite)
|