-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9dc750
commit 0908402
Showing
4 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .classes import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .test_timer import * |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import time | ||
import unittest | ||
|
||
from pyxx.dev import TimeIt | ||
from tests import CapturePrint | ||
|
||
|
||
class Test_TimeIt(unittest.TestCase): | ||
def test_duration(self): | ||
# Verifies that duration is measured within a reasonable range | ||
timer = TimeIt(print_duration=False) | ||
with timer: | ||
time.sleep(1.5) | ||
|
||
self.assertAlmostEqual(timer.duration(), 1.5, delta=0.1) | ||
|
||
def test_default_message(self): | ||
# Verifies that correct default message is printed after code completes | ||
with CapturePrint() as terminal_output: | ||
timer = TimeIt() | ||
with timer: | ||
time.sleep(0.1) | ||
|
||
self.assertEqual( | ||
terminal_output.getvalue(), | ||
f'Code duration: {timer.duration()} s\n', | ||
) | ||
|
||
def test_custom_message(self): | ||
# Verifies that correct custom message is printed after code completes | ||
with CapturePrint() as terminal_output: | ||
timer = TimeIt(message='Execution time: {time} {units}', units='ms') | ||
with timer: | ||
time.sleep(0.1) | ||
|
||
self.assertEqual( | ||
terminal_output.getvalue(), | ||
f'Execution time: {timer.duration() * 1000} ms\n', | ||
) | ||
|
||
def test_convert_duration(self): | ||
# Verifies that unit conversion of returned duration is performed correctly | ||
timer = TimeIt(print_duration=False) | ||
with timer: | ||
time.sleep(0.5) | ||
|
||
self.assertAlmostEqual( | ||
timer.duration(units='s') * 1000, | ||
timer.duration(units='ms'), | ||
) | ||
|
||
def test_invalid_units(self): | ||
# Verify that an error is raised when invalid units are specified | ||
with self.assertRaises(ValueError): | ||
with TimeIt(units='invalid'): | ||
pass |