Best Python code snippet using selene_python
test_kernels.py
Source:test_kernels.py
...14from gptorch.util import TensorType15data_dir = os.path.join(os.path.dirname(__file__), "data", "kernels")16class Kern(object):17 @classmethod18 def setup_class(cls, kernel_type):19 cls.kernel_type = kernel_type20 cls.x1 = TensorType(np.load(os.path.join(data_dir, "x1.npy")))21 cls.x2 = TensorType(np.load(os.path.join(data_dir, "x2.npy")))22 cls.n1, cls.d1 = cls.x1.data.numpy().shape23 cls.n2, cls.d2 = cls.x2.data.numpy().shape24 cls.kern = cls.kernel_type(cls.d1)25 cls.kern_str = cls.kern.__class__.__name__26 cls.kx_expected = np.load(os.path.join(data_dir, "{}_kx.npy".format(27 cls.kern_str)))28 cls.kx2_expected = np.load(os.path.join(data_dir, "{}_kx2.npy".format(29 cls.kern_str)))30 cls.kdiag_expected = np.load(os.path.join(data_dir, 31 "{}_kdiag.npy".format(cls.kern_str)))32 def test_add(self):33 """34 Test kernel addition operator35 """36 k1 = self.kern + self.kern37 k2 = kernels.Sum(self.kern, self.kern)38 k1x = k1.K(self.x1)39 k2x = k2.K(self.x1)40 assert all([x1 == x2 for x1, x2 in zip(k1x.flatten(), k2x.flatten())])41 def test_mul(self):42 """43 Test kernel product operator44 """45 k1 = self.kern * self.kern46 k2 = kernels.Product(self.kern, self.kern)47 k1x = k1.K(self.x1)48 k2x = k2.K(self.x1)49 assert all([x1 == x2 for x1, x2 in zip(k1x.flatten(), k2x.flatten())])50 def test_K(self):51 """52 Inputs are expected to be torch.autograd.Variables of 53 torch.DoubleTensors54 Output is expected to be a torch.autograd.Variable55 """56 kx_actual = self.kern.K(self.x1).data.numpy()57 kx2_actual = self.kern.K(self.x1, self.x2).data.numpy()58 kx2t_actual = self.kern.K(self.x2, self.x1).data.numpy()59 assert np.allclose(self.kx_expected, kx_actual)60 assert np.allclose(self.kx2_expected, kx2_actual)61 # Test symmetric K()62 assert np.allclose(kx_actual.T, kx_actual)63 # Test transpose of cross-kernel:64 assert np.allclose(self.kx2_expected, kx2t_actual.T)65 def test_Kdiag(self):66 kdiag_actual = self.kern.Kdiag(self.x1).data.numpy()67 assert np.allclose(self.kdiag_expected, kdiag_actual)68class Stationary(Kern):69 @classmethod70 def setup_class(cls, kernel_type):71 super(Stationary, cls).setup_class(kernel_type)72 x_shift = 0.3473 cls.x1_shift = cls.x1 + x_shift74 cls.x2_shift = cls.x1 + x_shift75 def test_K(self):76 super().test_K()77 kx_shift_actual = self.kern.K(self.x1_shift).data.numpy()78 assert np.allclose(self.kx_expected, kx_shift_actual)79 def test_Kdiag(self):80 super().test_Kdiag()81 kxdiag_shift_actual = self.kern.Kdiag(self.x1_shift).data.numpy()82 assert np.allclose(self.kdiag_expected, kxdiag_shift_actual)83class ARD(Stationary):84 @classmethod85 def setup_class(cls, kernel_type):86 super(ARD, cls).setup_class(kernel_type)87 cls.ard_length_scales = np.load(os.path.join(data_dir, 88 "ard_length_scales.npy"))89 cls.kern_ard = cls.kernel_type(cls.d1, ARD=True, 90 length_scales=cls.ard_length_scales)91 cls.kx_ard_expected = np.load(os.path.join(data_dir, 92 "{}_kx_ard.npy".format(cls.kern_str)))93 cls.kx2_ard_expected = np.load(os.path.join(data_dir, 94 "{}_kx2_ard.npy".format(cls.kern_str)))95 cls.kdiag_ard_expected = np.load(os.path.join(data_dir,96 "{}_kdiag_ard.npy".format(cls.kern_str)))97 def test_K(self):98 super().test_K()99 kx_ard_actual = self.kern_ard.K(self.x1).data.numpy()100 kx2_ard_actual = self.kern_ard.K(self.x1, self.x2).data.numpy()101 assert np.allclose(self.kx_ard_expected, kx_ard_actual)102 assert np.allclose(self.kx2_ard_expected, kx2_ard_actual)103 def test_Kdiag(self):104 super().test_Kdiag()105 kdiag_ard_actual = self.kern_ard.Kdiag(self.x1).data.numpy()106 assert np.allclose(self.kdiag_ard_expected, kdiag_ard_actual)107class TestWhite(Kern):108 @classmethod109 def setup_class(cls):110 super(TestWhite, cls).setup_class(kernels.White) 111class TestConstant(Kern):112 @classmethod113 def setup_class(cls):114 super(TestConstant, cls).setup_class(kernels.Constant) 115class TestBias(Kern):116 @classmethod117 def setup_class(cls):118 super(TestBias, cls).setup_class(kernels.Bias)119class TestExp(ARD):120 @classmethod121 def setup_class(cls):122 super(TestExp, cls).setup_class(kernels.Exp)123class TestMatern12(ARD):124 @classmethod125 def setup_class(cls):126 super(TestMatern12, cls).setup_class(kernels.Matern12)127class TestMatern32(ARD):128 @classmethod129 def setup_class(cls):130 super().setup_class(kernels.Matern32)131class TestMatern52(ARD):132 @classmethod133 def setup_class(cls):134 super().setup_class(kernels.Matern52)135class TestRbf(ARD):136 @classmethod137 def setup_class(cls):138 super().setup_class(kernels.Rbf)139class TestPeriodic(ARD):140 @classmethod141 def setup_class(cls):142 super().setup_class(kernels.Periodic)143class TestLinear(Kern):144 @classmethod145 def setup_class(cls):...
test_inventory.py
Source:test_inventory.py
...20 with pytest.raises(ValueError):21 object = MobileInventory({'iPhone Model X': -45, 'Xiaomi Model Y': 200, 'Nokia Model Z': 25})22class TestInventoryAddStock():23 @pytest.fixture(scope='module')24 def setup_class(self):25 self.inventory = MobileInventory({'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25})26 return self.inventory27 def test_add_new_stock_as_dict(self, setup_class):28 setup_class.add_stock({'iPhone Model X':50, 'Xiaomi Model Y': 2000, 'Nokia Model A':10})29 assert setup_class.balance_inventory == {'iPhone Model X':150, 'Xiaomi Model Y': 3000, 'Nokia Model Z':25, 'Nokia Model A':10}30 def test_add_new_stock_as_list(self, setup_class):31 with pytest.raises(TypeError):32 setup_class.add_stock(['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z'])33 def test_add_new_stock_with_numeric_keys(self, setup_class):34 with pytest.raises(ValueError):35 setup_class.add_stock({1:'iPhone Model A', 2:'Xiaomi Model B', 3:'Nokia Model C'})36 def test_add_new_stock_with_nonnumeric_values(self, setup_class):37 with pytest.raises(ValueError):38 setup_class.add_stock({'iPhone Model A':'50', 'Xiaomi Model B':'2000', 'Nokia Model C':'25'})39 def test_add_new_stock_with_float_values(self, setup_class):40 with pytest.raises(ValueError):41 setup_class.add_stock({'iPhone Model A':50.5, 'Xiaomi Model B':2000.3, 'Nokia Model C':25})42class TestInventorySellStock():43 @pytest.fixture(scope='module')44 def setup_class(self):45 self.inventory = MobileInventory({'iPhone Model A':50, 'Xiaomi Model B': 2000, 'Nokia Model C':10, 'Sony Model D':1})46 return self.inventory47 def test_sell_stock_as_dict(self, setup_class):48 setup_class.sell_stock({'iPhone Model A':2, 'Xiaomi Model B':20, 'Sony Model D':1})49 assert setup_class.balance_inventory == {'iPhone Model A':48, 'Xiaomi Model B': 1980, 'Nokia Model C':10, 'Sony Model D':0}50 def test_sell_stock_as_list(self, setup_class):51 with pytest.raises(TypeError):52 setup_class.sell_stock(['iPhone Model A', 'Xiaomi Model B', 'Nokia Model C'])53 def test_sell_stock_with_numeric_keys(self, setup_class):54 with pytest.raises(ValueError):55 setup_class.sell_stock({1:'iPhone Model A', 2:'Xiaomi Model B', 3:'Nokia Model C'})56 def test_sell_stock_with_nonnumeric_values(self, setup_class):57 with pytest.raises(ValueError):58 setup_class.sell_stock({'iPhone Model A':'2', 'Xiaomi Model B':'3', 'Nokia Model C':'4'})...
test_setup_flow_example.py
Source:test_setup_flow_example.py
1def setup_module(module):2 module.TestStateFullThing.classcount = 03class TestStateFullThing(object):4 def setup_class(cls):5 cls.classcount += 16 def teardown_class(cls):7 cls.classcount -= 18 def setup_method(self, method):9 self.id = eval(method.__name__[5:])10 def test_42(self):11 assert self.classcount == 112 assert self.id == 4213 def test_23(self):14 assert self.classcount == 115 assert self.id == 2316def teardown_module(module):17 assert module.TestStateFullThing.classcount == 018""" For this example the control flow happens as follows::19 import test_setup_flow_example20 setup_module(test_setup_flow_example)21 setup_class(TestStateFullThing)22 instance = TestStateFullThing()23 setup_method(instance, instance.test_42)24 instance.test_42()25 setup_method(instance, instance.test_23)26 instance.test_23()27 teardown_class(TestStateFullThing)28 teardown_module(test_setup_flow_example)29Note that ``setup_class(TestStateFullThing)`` is called and not30``TestStateFullThing.setup_class()`` which would require you31to insert ``setup_class = classmethod(setup_class)`` to make32your setup function callable....
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!!