Best Python code snippet using localstack_python
problem_unittests.py
Source:problem_unittests.py
1from unittest.mock import MagicMock, patch2import numpy as np3import torch4def _print_success_message():5 print('Tests Passed')6class AssertTest(object):7 def __init__(self, params):8 self.assert_param_message = '\n'.join([str(k) + ': ' + str(v) + '' for k, v in params.items()])9 def test(self, assert_condition, assert_message):10 assert assert_condition, assert_message + '\n\nUnit Test Function Parameters\n' + self.assert_param_message11 12def test_discriminator(Discriminator):13 batch_size = 5014 conv_dim=1015 D = Discriminator(conv_dim)16 # create random image input17 x = torch.from_numpy(np.random.randint(1, size=(batch_size, 3, 32, 32))*2 -1).float()18 19 train_on_gpu = torch.cuda.is_available()20 if train_on_gpu:21 x.cuda()22 output = D(x)23 assert_test = AssertTest({24 'Conv Dim': conv_dim,25 'Batch Size': batch_size,26 'Input': x})27 correct_output_size = (batch_size, 1)28 assert_condition = output.size() == correct_output_size29 assert_message = 'Wrong output size. Expected type {}. Got type {}'.format(correct_output_size, output.size())30 assert_test.test(assert_condition, assert_message)31 _print_success_message()32 33def test_generator(Generator):34 batch_size = 5035 z_size = 2536 conv_dim=1037 G = Generator(z_size, conv_dim)38 # create random input39 z = np.random.uniform(-1, 1, size=(batch_size, z_size))40 z = torch.from_numpy(z).float()41 42 train_on_gpu = torch.cuda.is_available()43 if train_on_gpu:44 z.cuda()45 #b = torch.LongTensor(a)46 #nn_input = torch.autograd.Variable(b)47 output = G(z)48 assert_test = AssertTest({49 'Z size': z_size,50 'Conv Dim': conv_dim,51 'Batch Size': batch_size,52 'Input': z})53 correct_output_size = (batch_size, 3, 32, 32)54 assert_condition = output.size() == correct_output_size55 assert_message = 'Wrong output size. Expected type {}. Got type {}'.format(correct_output_size, output.size())56 assert_test.test(assert_condition, assert_message)...
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!!