Best Python code snippet using Testify_python
test_assertions.py
Source:test_assertions.py  
...186        assert_datetimes_equal(t0, t1)187        t0 = datetime(1970, 1, 1)188        t1 = datetime(1970, 1, 1)189        assert_datetimes_equal(t0, t1)190    def test_assert_exactly_one(self):191        assert_exactly_one(None, False, None, None)192        assert_exactly_one(None, True, None, None)193class NegativeAssertionsTestCase(unittest.TestCase):194    """Test all assertions with the expectation of them all failing."""195    def test_assert_raises(self):196        class MyException(Exception):197            pass198        with assert_raises(AssertionError):199            with assert_raises(TypeError):200                raise MyException()201        with assert_raises(AssertionError):202            with assert_raises(Exception):203                pass204    def test_assert_raises_and_contains(self):205        def no_fail():206            return207        def fail():208            raise ValueError("choose one of the correct values")209        with assert_raises(AssertionError):210            assert_raises_and_contains(ValueError, "two of", fail)211        with assert_raises(AssertionError):212            assert_raises_and_contains(Exception, "anything", no_fail)213    def test_assert_equal(self):214        with assert_raises(AssertionError):215            assert_equal(1, 2)216    def test_assert_almost_equal(self):217        with assert_raises(AssertionError):218            assert_almost_equal(1, 1.01, 2)219    def test_assert_within_tolerance(self):220        with assert_raises(AssertionError):221            assert_within_tolerance(5, 5.1, 0.01)222    def test_assert_not_equal(self):223        with assert_raises(AssertionError):224            assert_not_equal(1, 1)225    def test_assert_lt(self):226        with assert_raises(AssertionError):227            assert_lt(3, 2)228    def test_assert_lte(self):229        with assert_raises(AssertionError):230            assert_lte(10, 1)231    def test_assert_gt(self):232        with assert_raises(AssertionError):233            assert_gt(1, 4)234    def test_assert_gte(self):235        with assert_raises(AssertionError):236            assert_gte(3, 5)237    def test_assert_in_range(self):238        with assert_raises(AssertionError):239            assert_in_range(1, 2, 4)240    def test_assert_between(self):241        with assert_raises(AssertionError):242            assert_between(1, 3, 2)243    def test_assert_in(self):244        with assert_raises(AssertionError):245            assert_in('a', [1, 2, 3])246    def test_assert_not_in(self):247        with assert_raises(AssertionError):248            assert_not_in(1, [1, 2, 3])249    def test_assert_all_in(self):250        with assert_raises(AssertionError):251            assert_all_in([1, 2], [1, 3])252    def test_assert_starts_with(self):253        with assert_raises(AssertionError):254            assert_starts_with('abc123', 'bc')255    def test_assert_not_reached(self):256        # The only way to test this assertion negatively is to not reach it :)257        pass258    def test_assert_rows_equal(self):259        with assert_raises(AssertionError):260            row1 = dict(a=1, b=2)261            row2 = dict(b=3, a=1)262            row3 = dict(b=1, a=1)263            assert_rows_equal([row1, row2], [row2, row3])264    def test_assert_length(self):265        with assert_raises(AssertionError):266            assert_length('abc', 4)267    def test_assert_is(self):268        with assert_raises(AssertionError):269            assert_is(True, False)270    def test_assert_is_not(self):271        with assert_raises(AssertionError):272            assert_is_not(True, True)273    def test_assert_all_match_regex(self):274        with assert_raises(AssertionError):275            values = [276                '$%`',277                '123 abc def',278            ]279            pattern = re.compile(r'\w+')280            assert_all_match_regex(pattern, values)281    def test_assert_match_regex(self):282        with assert_raises(AssertionError):283            pattern = re.compile(r'\w+')284            assert_match_regex(pattern, '$')285    def test_assert_any_match_regex(self):286        with assert_raises(AssertionError):287            values = [288                '"$',289                '@#~',290            ]291            pattern = re.compile(r'\w+')292            assert_any_match_regex(pattern, values)293    def test_assert_all_not_match_regex(self):294        with assert_raises(AssertionError):295            values = [296                '"$',297                'abc',298                '@#~',299            ]300            pattern = re.compile(r'\w+')301            assert_all_not_match_regex(pattern, values)302    def test_assert_sets_equal(self):303        with assert_raises(AssertionError):304            assert_sets_equal(set([1, 2, 3]), set([1, 'b', 'c']))305    def test_assert_dicts_equal(self):306        class A(object):307            pass308        with assert_raises(AssertionError):309            assert_dicts_equal(310                dict(a=[1, 2], b=dict(c=1), c=A(), d=(1, 2, 3)),311                dict(a=[1, 2, 3], b=dict(d=2), c=A(), d=(1, 2))312            )313    def test_assert_dict_subset(self):314        with assert_raises(AssertionError):315            assert_dict_subset(dict(a=2), dict(b=3))316    def test_assert_subset(self):317        with assert_raises(AssertionError):318            assert_subset(set([1, 2, 3]), set([1, 2]))319    def test_assert_list_prefix(self):320        with assert_raises(AssertionError):321            assert_list_prefix([1, 2, 3], [4, 5, 6])322    def test_assert_sorted_equal(self):323        with assert_raises(AssertionError):324            assert_sorted_equal([1, 2, 3], [3, 2, 3])325    def test_assert_isinstance(self):326        with assert_raises(AssertionError):327            assert_isinstance(dict(), list)328    def test_assert_datetimes_equal(self):329        with assert_raises(AssertionError):330            assert_datetimes_equal(datetime(1970, 1, 1), datetime.now())331    def test_assert_exactly_one(self):332        with assert_raises(AssertionError):333            assert_exactly_one(True, False, None, 1)334class FailureAssertionsTestCase(unittest.TestCase):335    """Throw garbage at assertions to trip them over."""336    def test_assert_starts_with(self):337        with assert_raises(TypeError):338            assert_starts_with(False, 'abc123')339        with assert_raises(TypeError):340            assert_starts_with('abc123', False)341if __name__ == '__main__':...test_helpers.py
Source:test_helpers.py  
...211        we get the expected return.212        We check for both True / False, and truthy / falsy values 'a' and '', and verify that213        they can safely be used in any combination.214        """215        def assert_exactly_one(true=0, truthy=0, false=0, falsy=0):216            sample = []217            for truth_value, num in [(True, true), (False, false), ('a', truthy), ('', falsy)]:218                if num:219                    sample.extend([truth_value] * num)220            if sample:221                expected = True if true + truthy == 1 else False222                assert exactly_one(*sample) is expected223        for row in product(range(4), range(4), range(4), range(4)):224            assert_exactly_one(*row)225    def test_exactly_one_should_fail(self):226        with pytest.raises(ValueError):227            exactly_one([True, False])228    @pytest.mark.parametrize(229        'mode, expected',230        [231            (232                'strict',233                {234                    'b': '',235                    'c': {'b': '', 'c': 'hi', 'd': ['', 0, '1']},236                    'd': ['', 0, '1'],237                    'e': ['', 0, {'b': '', 'c': 'hi', 'd': ['', 0, '1']}, ['', 0, '1'], ['']],238                },...dataloader.py
Source:dataloader.py  
...21        pin_memory=True,22        drop_last=False)23    return eval_loader24def create_subset_loaders(train_dataset, labeled_idxs, args):25    #assert_exactly_one([args.exclude_unlabeled, args.labeled_batch_size])26    sampler = SubsetRandomSampler(labeled_idxs)27    batch_sampler = BatchSampler(sampler, args.batch_size, drop_last=False)28    subset_loader = torch.utils.data.DataLoader(train_dataset,29                                               batch_sampler=batch_sampler,30                                               num_workers=args.workers,31                                               pin_memory=True,32                                               drop_last=False)33    return subset_loader34    35def create_two_stream_loaders(train_dataset, unlabeled_idxs, labeled_idxs, args):36    batch_sampler = data.TwoStreamBatchSampler(37            unlabeled_idxs, labeled_idxs, args.batch_size, args.labeled_batch_size)38    loader = torch.utils.data.DataLoader(train_dataset,39                                        batch_sampler=batch_sampler,...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!!
