Skip to content

Commit

Permalink
Merge pull request #51 from ayasyrev:fix_tests
Browse files Browse the repository at this point in the history
fix tests - add pytest approx
  • Loading branch information
ayasyrev authored Jul 20, 2024
2 parents 706364d + 23fe262 commit cf06b8d
Showing 1 changed file with 3 additions and 25 deletions.
28 changes: 3 additions & 25 deletions tests/test_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from multiprocessing import cpu_count
from time import sleep

import pytest
from pytest import CaptureFixture

from benchmark_utils import benchmark
Expand Down Expand Up @@ -50,29 +51,6 @@ def test_func_name():
assert func_name == "func_to_test_1(0.2, mult=2)"


def equal_near(item_1: float, item_2: float, threshold: float = 0.1) -> bool:
"""Is two item close to equal?
Return True is difference less than threshold.
Args:
item_1 (float): First item.
item_2 (float): Second item.
threshold (float, optional): Threshold for compare. Defaults to 0.01.
Returns:
bool: Return True if difference less than threshold.
"""
return abs(1 - (item_1 / item_2)) < threshold


def test_equal_near():
"""test for equal func"""
assert equal_near(1.0, 1.0099)
assert equal_near(1.0, 0.999)
assert not equal_near(1.0, 1.112)
assert not equal_near(1.0, 0.9)


def test_benchmark():
"""base tests for bench"""
name_func = "test_func_1"
Expand All @@ -86,7 +64,7 @@ def test_benchmark():
# ran as __call__
bench()
result = bench.results[name_func]
assert equal_near(result, sleep_time, threshold=0.5)
assert result == pytest.approx(sleep_time, 0.01)
assert name_func in str(bench)
assert bench._results is not None
assert len(bench._results) == 1
Expand Down Expand Up @@ -211,7 +189,7 @@ def test_benchmark_iter(capsys: CaptureFixture[str]):
captured = capsys.readouterr()
assert name_func in captured.out
result = bench.results[name_func]
assert equal_near(result, sleep_time * len_item_list, threshold=0.5)
assert result == pytest.approx(sleep_time * len_item_list, 0.05)
# check print
bench.print_results_per_item()
captured = capsys.readouterr()
Expand Down

0 comments on commit cf06b8d

Please sign in to comment.