Best Python code snippet using Testify_python
test_injector.py
Source:test_injector.py
1from asynctest import TestCase as AsyncTestCase2from ..base import BaseProvider, BaseInjector, BaseSettings, InjectionError3from ..injector import Injector4from ..provider import ClassProvider, CachedProvider5class MockProvider(BaseProvider):6 def __init__(self, value):7 self.settings = None8 self.injector = None9 self.value = value10 def provide(self, settings: BaseSettings, injector: BaseInjector):11 self.settings = settings12 self.injector = injector13 return self.value14class MockInstance:15 def __init__(self, value, **kwargs):16 self.opened = False17 self.value = value18 self.kwargs = kwargs19 def open(self):20 self.opened = True21class TestInjector(AsyncTestCase):22 def setUp(self):23 self.test_key = "TEST"24 self.test_value = "VALUE"25 self.test_settings = {self.test_key: self.test_value}26 self.test_instance = Injector(settings=self.test_settings)27 assert self.test_instance.__class__.__name__ in str(self.test_instance)28 def test_settings_init(self):29 """Test settings initialization."""30 for key in self.test_settings:31 assert key in self.test_instance.settings32 assert self.test_instance.settings[key] == self.test_settings[key]33 def test_inject_simple(self):34 """Test a basic injection."""35 assert self.test_instance.inject(str, required=False) is None36 with self.assertRaises(InjectionError):37 self.test_instance.inject(str)38 with self.assertRaises(ValueError):39 self.test_instance.bind_instance(str, None)40 self.test_instance.bind_instance(str, self.test_value)41 assert self.test_instance.inject(str) is self.test_value42 def test_inject_x(self):43 """Test injection failure on null base class."""44 with self.assertRaises(InjectionError):45 self.test_instance.inject(None)46 def test_inject_provider(self):47 """Test a provider injection."""48 mock_provider = MockProvider(self.test_value)49 with self.assertRaises(ValueError):50 self.test_instance.bind_provider(str, None)51 self.test_instance.bind_provider(str, mock_provider)52 assert self.test_instance.get_provider(str) is mock_provider53 override_settings = {self.test_key: "NEWVAL"}54 assert self.test_instance.inject(str, override_settings) is self.test_value55 assert mock_provider.settings[self.test_key] == override_settings[self.test_key]56 assert mock_provider.injector is self.test_instance57 def test_bad_provider(self):58 """Test empty and invalid provider results."""59 self.test_instance.bind_provider(str, MockProvider(None))60 with self.assertRaises(InjectionError):61 self.test_instance.inject(str)62 self.test_instance.inject(str, required=False)63 self.test_instance.bind_provider(str, MockProvider(1))64 self.test_instance.clear_binding(str)65 assert self.test_instance.get_provider(str) is None66 with self.assertRaises(InjectionError):67 self.test_instance.inject(str)68 def test_inject_class(self):69 """Test a provider class injection."""70 provider = ClassProvider(MockInstance, self.test_value, init_method="open")71 self.test_instance.bind_provider(MockInstance, provider)72 assert self.test_instance.get_provider(MockInstance) is provider73 instance = self.test_instance.inject(MockInstance)74 assert isinstance(instance, MockInstance)75 assert instance.value is self.test_value76 assert instance.opened77 def test_inject_class_name(self):78 """Test a provider class injection with a named class."""79 provider = ClassProvider("aries_cloudagent.config.settings.Settings")80 self.test_instance.bind_provider(BaseSettings, provider)81 instance = self.test_instance.inject(BaseSettings)82 assert (83 isinstance(instance, BaseSettings)84 and instance.__class__.__name__ == "Settings"85 )86 def test_inject_class_dependency(self):87 """Test a provider class injection with a dependency."""88 test_str = "TEST"89 test_int = 190 self.test_instance.bind_instance(str, test_str)91 self.test_instance.bind_instance(int, test_int)92 provider = ClassProvider(93 MockInstance, ClassProvider.Inject(str), param=ClassProvider.Inject(int)94 )95 self.test_instance.bind_provider(object, provider)96 instance = self.test_instance.inject(object)97 assert instance.value is test_str98 assert instance.kwargs["param"] is test_int99 self.test_instance.clear_binding(int)100 self.test_instance.clear_binding(str)101 self.test_instance.bind_instance(str, test_int)102 with self.assertRaises(InjectionError):103 self.test_instance.inject(str)104 def test_inject_cached(self):105 """Test a provider class injection."""106 with self.assertRaises(ValueError):107 CachedProvider(None)108 provider = ClassProvider(MockInstance, self.test_value, init_method="open")109 cached = CachedProvider(provider)110 self.test_instance.bind_provider(MockInstance, cached)111 assert self.test_instance.get_provider(MockInstance) is cached112 i1 = self.test_instance.inject(MockInstance)113 i2 = self.test_instance.inject(MockInstance)114 assert i1 is i2115 provider = ClassProvider(MockInstance, self.test_value, init_method="open")116 self.test_instance.bind_provider(MockInstance, provider, cache=True)117 i1 = self.test_instance.inject(MockInstance)118 i2 = self.test_instance.inject(MockInstance)...
data_access_view_tests.py
Source:data_access_view_tests.py
1from rest_framework.authtoken.models import Token2from rest_framework.test import APIClient3from django.urls import reverse4from ClassSchedulerBackend.models import User5from .test_utils import *6class DAVTestTemplate:7 url_name = None8 add_data = None9 id = None10 update_data = None11 delete_data = None12 field_compare = None13 client_class = APIClient14 data_format = "json"15 user = None16 token = None17 auth_header = None18 @staticmethod19 def setUpTestData(cls):20 credentials = {21 'username': 'admin',22 'password': 'admin'23 }24 cls.user = User.objects.get(username=credentials["username"])25 cls.token = Token.objects.get(user_id=cls.user.pk).key26 cls.auth_header = {"HTTP_AUTHORIZATION": "Token {}".format(cls.token)}27 @classmethod28 def setUpdateData(cls, test_instance):29 print(cls.add_data)30 cls.update_data = cls.add_data31 cls.update_data["id"] = decode_content(cls.__get_response(test_instance).content)[0]["id"]32 @classmethod33 def __get_response(cls, test_instance):34 return test_instance.client.get(35 reverse(cls.url_name),36 format=cls.data_format,37 **cls.auth_header38 )39 @classmethod40 def __add_response(cls, test_instance):41 return test_instance.client.post(42 reverse(cls.url_name),43 format=cls.data_format,44 data=cls.add_data,45 **cls.auth_header46 )47 @classmethod48 def __update_response(cls, test_instance):49 cls.setUpdateData(test_instance)50 return test_instance.client.put(51 reverse(cls.url_name),52 format=cls.data_format,53 data=cls.update_data,54 **cls.auth_header55 )56 @classmethod57 def __delete_response(cls, test_instance):58 return test_instance.client.delete(59 reverse(cls.url_name),60 format=cls.data_format,61 data=cls.delete_data,62 **cls.auth_header63 )64 @classmethod65 def _test_get(cls, test_instance):66 cls.__add_response(test_instance)67 response = cls.__get_response(test_instance)68 print(response.content)69 test_instance.assertEqual(response.status_code, 200)70 test_instance.assertTrue(len(decode_content(response.content)) > 0)71 @classmethod72 def _test_add(cls, test_instance):73 response = cls.__add_response(test_instance)74 print(response.content)75 test_instance.assertEqual(response.status_code, 200)76 test_instance.assertEqual(decode_content(response.content)[cls.field_compare], cls.add_data[cls.field_compare])77 @classmethod78 def _test_update(cls, test_instance):79 try:80 response = cls.__update_response(test_instance)81 print(response.content)82 except IndexError:83 cls.__add_response(test_instance)84 response = cls.__update_response(test_instance)85 print(response.content)86 test_instance.assertEqual(response.status_code, 200)87 test_instance.assertEqual(decode_content(response.content)[cls.field_compare], cls.add_data[cls.field_compare])88 @classmethod89 def _test_delete(cls, test_instance):90 cls.__add_response(test_instance)91 response = cls.__delete_response(test_instance)92 print(response.content)93 test_instance.assertEqual(response.status_code, 200)...
test_run_factory.py
Source:test_run_factory.py
1# Copyright 2014 The Chromium Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.4from pylib.gtest import gtest_test_instance5from pylib.instrumentation import instrumentation_test_instance6from pylib.junit import junit_test_instance7from pylib.linker import linker_test_instance8from pylib.monkey import monkey_test_instance9from pylib.local.device import local_device_environment10from pylib.local.device import local_device_gtest_run11from pylib.local.device import local_device_instrumentation_test_run12from pylib.local.device import local_device_linker_test_run13from pylib.local.device import local_device_monkey_test_run14from pylib.local.device import local_device_perf_test_run15from pylib.local.machine import local_machine_environment16from pylib.local.machine import local_machine_junit_test_run17from pylib.perf import perf_test_instance18def _CreatePerfTestRun(args, env, test_instance):19 if args.print_step:20 return local_device_perf_test_run.PrintStep(21 env, test_instance)22 elif args.output_json_list:23 return local_device_perf_test_run.OutputJsonList(24 env, test_instance)25 return local_device_perf_test_run.LocalDevicePerfTestRun(26 env, test_instance)27def CreateTestRun(args, env, test_instance, error_func):28 if isinstance(env, local_device_environment.LocalDeviceEnvironment):29 if isinstance(test_instance, gtest_test_instance.GtestTestInstance):30 return local_device_gtest_run.LocalDeviceGtestRun(env, test_instance)31 if isinstance(test_instance,32 instrumentation_test_instance.InstrumentationTestInstance):33 return (local_device_instrumentation_test_run34 .LocalDeviceInstrumentationTestRun(env, test_instance))35 if isinstance(test_instance, linker_test_instance.LinkerTestInstance):36 return (local_device_linker_test_run37 .LocalDeviceLinkerTestRun(env, test_instance))38 if isinstance(test_instance, monkey_test_instance.MonkeyTestInstance):39 return (local_device_monkey_test_run40 .LocalDeviceMonkeyTestRun(env, test_instance))41 if isinstance(test_instance,42 perf_test_instance.PerfTestInstance):43 return _CreatePerfTestRun(args, env, test_instance)44 if isinstance(env, local_machine_environment.LocalMachineEnvironment):45 if isinstance(test_instance, junit_test_instance.JunitTestInstance):46 return (local_machine_junit_test_run47 .LocalMachineJunitTestRun(env, test_instance))48 error_func('Unable to create test run for %s tests in %s environment'...
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!!