Skip to content
This repository was archived by the owner on Jul 26, 2020. It is now read-only.

Commit f1e0154

Browse files
committed
use pytest.approx for approximations
1 parent adb3cea commit f1e0154

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

tests/test_coordinates_from_file.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pathlib import Path
55

66
import pytest
7+
from pytest import approx
78

89
from sb_vision import Cartesian, FileCamera, Spherical, Vision
910

@@ -37,11 +38,6 @@
3738
]
3839

3940

40-
def assertWithinTolerance(val, expected, tolerance, error_msg=""):
41-
"""Assert that the value is within a certain tolerance value."""
42-
assert (expected - tolerance) < val < (expected + tolerance), error_msg
43-
44-
4541
@pytest.mark.parametrize(
4642
"photo, expected_cartesian, expected_spherical",
4743
TEST_IMAGES,
@@ -57,16 +53,10 @@ def test_image_coordinates(photo, expected_cartesian, expected_spherical):
5753

5854
tolerance = dist * DIST_PERCENT_TOLERANCE
5955

60-
assertWithinTolerance(x, expected_cartesian.x, tolerance,
61-
"Wrong x-coordinate")
62-
assertWithinTolerance(y, expected_cartesian.y, tolerance,
63-
"Wrong y-coordinate")
64-
assertWithinTolerance(z, expected_cartesian.z, tolerance,
65-
"Wrong z-coordinate")
56+
assert x == approx(expected_cartesian.x, abs=tolerance), "Wrong x-coordinate"
57+
assert y == approx(expected_cartesian.y, abs=tolerance), "Wrong y-coordinate"
58+
assert z == approx(expected_cartesian.z, abs=tolerance), "Wrong z-coordinate"
6659

67-
assertWithinTolerance(rot_x, expected_spherical.rot_x, ROTATION_TOLERANCE,
68-
"Wrong x rotation")
69-
assertWithinTolerance(rot_y, expected_spherical.rot_y, ROTATION_TOLERANCE,
70-
"Wrong y rotation")
71-
assertWithinTolerance(dist, expected_spherical.dist, tolerance,
72-
"Wrong distance")
60+
assert rot_x == approx(expected_spherical.rot_x, abs=tolerance), "Wrong x-coordinate"
61+
assert rot_y == approx(expected_spherical.rot_y, abs=tolerance), "Wrong y-coordinate"
62+
assert dist == approx(expected_spherical.dist, abs=tolerance), "Wrong distance"

tests/test_marker_sizes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from typing import Dict, Tuple
55
from unittest import mock
66

7+
from pytest import approx
8+
79
from sb_vision import FileCamera, Vision
810
from sb_vision.camera_base import CameraBase
911

@@ -34,7 +36,7 @@ def assertMarkerDistance(
3436
token, = vision.snapshot()
3537

3638
dist = token.spherical.dist
37-
assertWithinToleranceRatio(dist, expected_distance, EXPECTED_TOLERANCE)
39+
assert dist == approx(expected_distance, rel=EXPECTED_TOLERANCE)
3840

3941

4042
def test_unknown_marker_size():

0 commit comments

Comments
 (0)