Best Python code snippet using Testify_python
test_timer.py
Source:test_timer.py  
...13import torch.distributed.launcher as launcher14from torchtnt.utils.test_utils import get_pet_launch_config15from torchtnt.utils.timer import FullSyncPeriodicTimer, Timer16class TimerTest(unittest.TestCase):17    def assert_within_tolerance(18        self, expected: float, actual: float, percent_tolerance: float = 1019    ) -> None:20        """Assert that a value is correct within a percent tolerance"""21        error = abs(expected - actual)22        tolerance = expected * (percent_tolerance / 100)23        self.assertLess(error, tolerance)24    def test_timer_basic(self) -> None:25        """Basic test of the timer class"""26        # Generate 3 intervals between 0.5 and 2 seconds27        intervals = [(random() * 1.5) + 0.5 for _ in range(3)]28        # Basic start and stop test29        timer = Timer()30        timer.start()31        time.sleep(intervals[0])32        timer.stop()33        self.assertEqual(timer.interval_time_seconds, timer.total_time_seconds)34        self.assert_within_tolerance(timer.total_time_seconds, intervals[0])35        total = timer.total_time_seconds36        # Test that interval time resets and total time accumulates37        timer.start()38        time.sleep(intervals[1])39        timer.stop()40        self.assertLess(timer.interval_time_seconds, timer.total_time_seconds)41        self.assert_within_tolerance(timer.interval_time_seconds, intervals[1])42        self.assert_within_tolerance(timer.total_time_seconds, total + intervals[1])43        # Test that reset works properly44        timer.reset()45        timer.start()46        time.sleep(intervals[2])47        timer.stop()48        self.assertEqual(timer.interval_time_seconds, timer.total_time_seconds)49        self.assert_within_tolerance(timer.total_time_seconds, intervals[2])50    def test_extra_starts_stops(self) -> None:51        """Test behavior with extra starts and stops"""52        # Generate 2 intervals between 0.5 and 2 seconds53        intervals = [(random() * 1.5) + 0.5 for _ in range(2)]54        # Test multiple starts55        timer = Timer()56        timer.start()57        time.sleep(intervals[0])58        with self.assertWarns(Warning):59            timer.start()60        timer.stop()61        self.assertEqual(timer.interval_time_seconds, timer.total_time_seconds)62        self.assert_within_tolerance(timer.total_time_seconds, intervals[0])63        total = timer.total_time_seconds64        # Test multiple stops65        timer.start()66        time.sleep(intervals[1])67        timer.stop()68        with self.assertWarns(Warning):69            timer.stop()70        self.assertLess(timer.interval_time_seconds, timer.total_time_seconds)71        self.assert_within_tolerance(timer.total_time_seconds, total + intervals[1])72    def test_missing_starts_stops(self) -> None:73        """Test behavior with missing starts and stops"""74        # Generate 1 interval between 0.5 and 2 seconds75        intervals = [(random() * 1.5) + 0.5 for _ in range(1)]76        timer = Timer()77        # Test stop without start78        timer.reset()79        with self.assertWarns(Warning):80            timer.stop()81        self.assertEqual(timer.interval_time_seconds, 0)82        self.assertEqual(timer.total_time_seconds, 0)83        # Test start without stop84        timer.reset()85        timer.start()86        time.sleep(intervals[0])87        # Saving values outside of asserts to reduce error from overhead88        interval_time = timer.interval_time_seconds89        total_time = timer.total_time_seconds90        self.assert_within_tolerance(interval_time, intervals[0])91        self.assert_within_tolerance(total_time, intervals[0])92    def test_timer_state_dict(self) -> None:93        """Test the statefulness of the timer class"""94        # Generate 3 intervals between 0.5 and 2 seconds95        intervals = [(random() * 1.5) + 0.5 for _ in range(3)]96        # Test saving state dict97        timer = Timer()98        timer.start()99        time.sleep(intervals[0])100        timer.stop()101        interval = timer.interval_time_seconds102        total = timer.total_time_seconds103        state_dict = timer.state_dict()104        self.assertEqual(len(state_dict), 3)105        self.assertIn("interval_start_time", state_dict)106        self.assertIn("interval_stop_time", state_dict)107        self.assertIn("total_time_seconds", state_dict)108        # Test loading state dict, ensure interval is preserved and total accumulates109        del timer110        timer = Timer()111        timer.load_state_dict(state_dict)112        self.assert_within_tolerance(timer.interval_time_seconds, interval)113        timer.start()114        time.sleep(intervals[1])115        timer.stop()116        self.assert_within_tolerance(timer.total_time_seconds, total + intervals[1])117        total = timer.total_time_seconds118        # Test saving state dict on running timer, ensure timer is paused119        timer.start()120        time.sleep(intervals[2])121        with self.assertRaisesRegex(122            Exception, "Timer must be paused before creating state_dict."123        ):124            state_dict = timer.state_dict()125    def test_timer_context_manager(self) -> None:126        """Test the context manager in the timer class"""127        # Generate 3 intervals between 0.5 and 2 seconds128        intervals = [(random() * 1.5) + 0.5 for _ in range(3)]129        # Basic test of context manager130        timer = Timer()131        with timer.time():132            time.sleep(intervals[0])133        self.assertEqual(timer.interval_time_seconds, timer.total_time_seconds)134        self.assert_within_tolerance(timer.total_time_seconds, intervals[0])135        total = timer.total_time_seconds136        # Ensure total accumulates with multiple context managers137        with timer.time():138            time.sleep(intervals[1])139        self.assertLess(timer.interval_time_seconds, timer.total_time_seconds)140        self.assert_within_tolerance(timer.interval_time_seconds, intervals[1])141        self.assert_within_tolerance(timer.total_time_seconds, total + intervals[1])142        total = timer.total_time_seconds143        # Make sure nested context managers work properly144        with self.assertWarns(Warning):145            with timer.time():146                with timer.time():147                    time.sleep(intervals[2])148        self.assertLess(timer.interval_time_seconds, timer.total_time_seconds)149        self.assert_within_tolerance(timer.interval_time_seconds, intervals[2])150        self.assert_within_tolerance(timer.total_time_seconds, total + intervals[2])151    @unittest.skipUnless(152        condition=torch.cuda.is_available(), reason="This test needs a GPU host to run."153    )154    def test_timer_synchronize(self) -> None:155        """Make sure that torch.cuda.synchronize() is called when GPU is present."""156        start_event = torch.cuda.Event(enable_timing=True)157        end_event = torch.cuda.Event(enable_timing=True)158        timer = Timer()159        # Do not explicitly call synchronize, timer must call it for test to pass.160        timer.start()161        start_event.record()162        time.sleep(0.5)163        end_event.record()164        timer.stop()165        # torch.cuda.synchronize() has to be called to compute the elapsed time.166        # Otherwise, there will be runtime error.167        elapsed_time_ms = start_event.elapsed_time(end_event)168        self.assertEqual(timer.interval_time_seconds, timer.total_time_seconds)169        self.assert_within_tolerance(timer.total_time_seconds, 0.5)170        self.assert_within_tolerance(timer.total_time_seconds, elapsed_time_ms / 1000)171class FullSyncPeriodicTimerTest(unittest.TestCase):172    @classmethod173    def _full_sync_worker_without_timeout(cls):174        dist.init_process_group("gloo")175        process_group = dist.group.WORLD176        interval_threshold = timedelta(seconds=5)177        fsp_timer = FullSyncPeriodicTimer(interval_threshold, process_group)178        return fsp_timer.check()179    @classmethod180    def _full_sync_worker_with_timeout(cls, timeout: int):181        dist.init_process_group("gloo")182        process_group = dist.group.WORLD183        interval_threshold = timedelta(seconds=5)184        fsp_timer = FullSyncPeriodicTimer(interval_threshold, process_group)...aliases.py
Source:aliases.py  
1#!/usr/bin/env python 2# -*- coding: utf-8 -*-3"""Some abbreviated shortcuts to common assertions.4For particularly lazy people who would rather type::5    import testy as t6    with t.raises(AssertionError):7        t.lt(3, 2)8than::9    from testy.assertions import assert_raises, assert_lt10    with assert_raises(AssertionError):11        assert_lt(3, 2)12"""13from __future__ import absolute_import14from .assertions import (15    assert_raises, assert_raises_and_contains, assert_equal,16    assert_almost_equal, assert_within_tolerance, assert_not_equal, assert_lt,17    assert_lte, assert_gt, assert_gte, assert_in_range, assert_between,18    assert_in, assert_not_in, assert_all_in, assert_starts_with,19    assert_not_reached, assert_rows_equal, assert_length,20    assert_is, assert_is_not, assert_all_match_regex, assert_match_regex,21    assert_any_match_regex, assert_all_not_match_regex, assert_sets_equal,22    assert_dicts_equal, assert_dict_subset, assert_subset, assert_list_prefix,23    assert_sorted_equal, assert_isinstance, assert_datetimes_equal,24    assert_exactly_one25)26raises = assert_raises27eq = assert_equal28equals = eq29equal = eq30ne = assert_not_equal31not_equal = ne32lt = assert_lt33lte = assert_lte34gt = assert_gt35gte = assert_gte36in_range = assert_in_range37between = in_range38in_seq = assert_in39not_in_seq = assert_not_in40not_in = not_in_seq41all_in = assert_all_in...__init__.py
Source:__init__.py  
1#!/usr/bin/env python 2# -*- coding: utf-8 -*-3from __future__ import absolute_import4__title__ = 'testy'5__version__ = '0.4'6__description__ = 'Python unittest helpers adapted from Testify'7__url__ = 'https://github.com/jimr/testy'8__author__ = 'James Rutherford'9__licence__ = 'MIT'10__copyright__ = 'Copyright 2012 James Rutherford'11from .assertions import (12    assert_raises, assert_raises_and_contains, assert_equal,13    assert_almost_equal, assert_within_tolerance, assert_not_equal, assert_lt,14    assert_lte, assert_gt, assert_gte, assert_in_range, assert_between,15    assert_in, assert_not_in, assert_all_in, assert_starts_with,16    assert_not_reached, assert_rows_equal, assert_length,17    assert_is, assert_is_not, assert_all_match_regex, assert_match_regex,18    assert_any_match_regex, assert_all_not_match_regex, assert_sets_equal,19    assert_dicts_equal, assert_dict_subset, assert_subset, assert_list_prefix,20    assert_sorted_equal, assert_isinstance, assert_datetimes_equal,21    assert_exactly_one22)23from .aliases import (24    raises, eq, equals, equal, ne, not_equal, lt, lte, gt, gte, in_range,25    between, in_seq, not_in_seq, not_in, all_in, regex, ...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
