Best Python code snippet using tox_python
test_jdict.py
Source:test_jdict.py
...4@pytest.fixture5def in_data():6 return {"x": 3, "y": 4, "z": 5}7@pytest.fixture8def nonempty(in_data):9 return jdict(in_data)10@pytest.fixture11def empty():12 return jdict()13def test_constructors(in_data):14 empty = jdict()15 empty_from_empty_dict = jdict({})16 nonempty_from_dict = jdict(in_data)17 nonempty_from_kwargs = jdict(x=3, y=4, z=5)18 assert len(empty) == 019 assert len(empty_from_empty_dict) == 020 assert len(nonempty_from_dict) == 321 assert len(nonempty_from_kwargs) == 322 assert empty.data == {}...
test_django_utils_translation.py
Source:test_django_utils_translation.py
1# -*- coding: utf-8 -*-2"""3Test methods exposed in common/lib/monkey_patch/django_utils_translation.py4Verify that the Django translation functions (gettext, ngettext,5pgettext, ugettext, and derivatives) all return the correct values6before, during, and after monkey-patching the django.utils.translation7module.8gettext, ngettext, pgettext, and ugettext must return a translation as9output for nonempty input.10ngettext, pgettext, npgettext, and ungettext must return an empty string11for an empty string as input.12gettext and ugettext will return translation headers, before and after13patching.14gettext and ugettext must return the empty string for any falsey input,15while patched.16*_noop must return the input text.17*_lazy must return the same text as their non-lazy counterparts.18"""19# pylint: disable=invalid-name20# Let names like `gettext_*` stay lowercase; makes matching easier.21# pylint: disable=missing-docstring22# All major functions are documented, the rest are self-evident shells.23# pylint: disable=no-member24# Pylint doesn't see our decorator `translate_with` add the `_` method.25from unittest import TestCase26from ddt import data27from ddt import ddt28from django.utils.translation import _trans29from django.utils.translation import gettext30from django.utils.translation import gettext_lazy31from django.utils.translation import gettext_noop32from django.utils.translation import ngettext33from django.utils.translation import ngettext_lazy34from django.utils.translation import npgettext35from django.utils.translation import npgettext_lazy36from django.utils.translation import pgettext37from django.utils.translation import pgettext_lazy38from django.utils.translation import ugettext39from django.utils.translation import ugettext_lazy40from django.utils.translation import ugettext_noop41from django.utils.translation import ungettext42from django.utils.translation import ungettext_lazy43from monkey_patch.django_utils_translation import ATTRIBUTES as attributes_patched44from monkey_patch.django_utils_translation import is_patched45from monkey_patch.django_utils_translation import patch46from monkey_patch.django_utils_translation import unpatch47# Note: The commented-out function names are explicitly excluded, as48# they are not attributes of `django.utils.translation._trans`.49# https://github.com/django/django/blob/1.4.8/django/utils/translation/__init__.py#L6950attributes_not_patched = [51 'gettext_noop',52 'ngettext',53 'npgettext',54 'pgettext',55 'ungettext',56 # 'gettext_lazy',57 # 'ngettext_lazy',58 # 'npgettext_lazy',59 # 'pgettext_lazy',60 # 'ugettext_lazy',61 # 'ugettext_noop',62 # 'ungettext_lazy',63]64class MonkeyPatchTest(TestCase):65 def setUp(self):66 """67 Remember the current state, then reset68 """69 self.was_patched = unpatch()70 self.unpatch_all()71 self.addCleanup(self.cleanup)72 def cleanup(self):73 """74 Revert translation functions to previous state75 Since the end state varies, we always unpatch to remove any76 changes, then repatch again iff the module was already77 patched when the test began.78 """79 self.unpatch_all()80 if self.was_patched:81 patch()82 def unpatch_all(self):83 """84 Unpatch the module recursively85 """86 while is_patched():87 unpatch()88@ddt89class PatchTest(MonkeyPatchTest):90 """91 Verify monkey-patching and un-monkey-patching92 """93 @data(*attributes_not_patched)94 def test_not_patch(self, attribute_name):95 """96 Test that functions are not patched unintentionally97 """98 self.unpatch_all()99 old_attribute = getattr(_trans, attribute_name)100 patch()101 new_attribute = getattr(_trans, attribute_name)102 self.assertIs(old_attribute, new_attribute)103 @data(*attributes_patched)104 def test_unpatch(self, attribute):105 """106 Test that unpatch gracefully handles unpatched functions107 """108 patch()109 self.assertTrue(is_patched())110 self.unpatch_all()111 self.assertFalse(is_patched())112 old_attribute = getattr(_trans, attribute)113 self.unpatch_all()114 new_attribute = getattr(_trans, attribute)115 self.assertIs(old_attribute, new_attribute)116 self.assertFalse(is_patched())117 @data(*attributes_patched)118 def test_patch_attributes(self, attribute):119 """120 Test that patch changes the attribute121 """122 self.unpatch_all()123 self.assertFalse(is_patched())124 old_attribute = getattr(_trans, attribute)125 patch()126 new_attribute = getattr(_trans, attribute)127 self.assertIsNot(old_attribute, new_attribute)128 self.assertTrue(is_patched())129 old_attribute = getattr(_trans, attribute)130 patch()131 new_attribute = getattr(_trans, attribute)132 self.assertIsNot(old_attribute, new_attribute)133 self.assertTrue(is_patched())134def translate_with(function):135 """136 Decorate a class by setting its `_` translation function137 """138 def decorate(cls):139 def _(self, *args):140 # pylint: disable=unused-argument141 return function(*args)142 cls._ = _143 return cls144 return decorate145@translate_with(ugettext)146class UgettextTest(MonkeyPatchTest):147 """148 Test a Django translation function149 Here we consider `ugettext` to be the base/default case. All other150 translation functions extend, as needed.151 """152 is_unicode = True153 needs_patched = True154 header = 'Project-Id-Version: '155 def setUp(self):156 """157 Restore translation text and functions158 """159 super(UgettextTest, self).setUp()160 if self.is_unicode:161 self.empty = u''162 self.nonempty = u'(â¯Â°â¡Â°ï¼â¯ï¸µ â»ââ»'163 else:164 self.empty = ''165 self.nonempty = 'Hey! Where are you?!'166 def assert_translations(self):167 """168 Assert that the empty and nonempty translations are correct169 The `empty = empty[:]` syntax is intentional. Since subclasses170 may implement a lazy translation, we must perform a "string171 operation" to coerce it to a string value. We don't use `str` or172 `unicode` because we also assert the string type.173 """174 empty, nonempty = self.get_translations()175 empty = empty[:]176 nonempty = nonempty[:]177 if self.is_unicode:178 self.assertTrue(isinstance(empty, unicode))179 self.assertTrue(isinstance(nonempty, unicode))180 else:181 self.assertTrue(isinstance(empty, str))182 self.assertTrue(isinstance(nonempty, str))183 if self.needs_patched and not is_patched():184 self.assertIn(self.header, empty)185 else:186 self.assertNotIn(self.header, empty)187 self.assertNotIn(self.header, nonempty)188 def get_translations(self):189 """190 Translate the empty and nonempty strings, per `self._`191 """192 empty = self._(self.empty)193 nonempty = self._(self.nonempty)194 return (empty, nonempty)195 def test_patch(self):196 """197 Test that `self._` correctly translates text before, during, and198 after being monkey-patched.199 """200 self.assert_translations()201 was_successful = patch()202 self.assertTrue(was_successful)203 self.assert_translations()204 was_successful = unpatch()205 self.assertTrue(was_successful)206 self.assert_translations()207@translate_with(gettext)208class GettextTest(UgettextTest):209 is_unicode = False210@translate_with(pgettext)211class PgettextTest(UgettextTest):212 needs_patched = False213 l18n_context = 'monkey_patch'214 def get_translations(self):215 empty = self._(self.l18n_context, self.empty)216 nonempty = self._(self.l18n_context, self.nonempty)217 return (empty, nonempty)218@translate_with(ngettext)219class NgettextTest(GettextTest):220 number = 1221 needs_patched = False222 def get_translations(self):223 empty = self._(self.empty, self.empty, self.number)224 nonempty = self._(self.nonempty, self.nonempty, self.number)225 return (empty, nonempty)226@translate_with(npgettext)227class NpgettextTest(PgettextTest):228 number = 1229 def get_translations(self):230 empty = self._(self.l18n_context, self.empty, self.empty, self.number)231 nonempty = self._(self.l18n_context, self.nonempty, self.nonempty, self.number)232 return (empty, nonempty)233class NpgettextPluralTest(NpgettextTest):234 number = 2235class NgettextPluralTest(NgettextTest):236 number = 2237@translate_with(gettext_noop)238class GettextNoopTest(GettextTest):239 needs_patched = False240@translate_with(ugettext_noop)241class UgettextNoopTest(UgettextTest):242 needs_patched = False243@translate_with(ungettext)244class UngettextTest(NgettextTest):245 is_unicode = True246class UngettextPluralTest(UngettextTest):247 number = 2248@translate_with(gettext_lazy)249class GettextLazyTest(GettextTest):250 pass251@translate_with(ugettext_lazy)252class UgettextLazyTest(UgettextTest):253 pass254@translate_with(pgettext_lazy)255class PgettextLazyTest(PgettextTest):256 pass257@translate_with(ngettext_lazy)258class NgettextLazyTest(NgettextTest):259 pass260@translate_with(npgettext_lazy)261class NpgettextLazyTest(NpgettextTest):262 pass263class NpgettextLazyPluralTest(NpgettextLazyTest):264 number = 2265@translate_with(ungettext_lazy)266class UngettextLazyTest(UngettextTest):...
smoketest.py
Source:smoketest.py
1import os2def error(message):3 print('\x1b[31m{}\x1b[0m'.format(message))4def check_if_nonempty(path):5 if not os.path.exists(path):6 error('ERROR: %s does not exist' % path)7 return False8 with open(path) as reader:9 data = reader.read().strip()10 if not data:11 error('ERROR: %s is empty' % path)12 return False13 return True14def smoketest():15 os.chdir(os.path.dirname(os.path.abspath(__file__)))16 if all([17 check_if_nonempty('q1/q1a.py'),18 check_if_nonempty('q1/q1a.txt'),19 check_if_nonempty('q1/q1b.py'),20 check_if_nonempty('q1/q1b.txt'),21 check_if_nonempty('q1/q1c.txt'),22 check_if_nonempty('q1/q1d.py'),23 check_if_nonempty('q1/q1d.txt'),24 check_if_nonempty('q2/a/winston.py'),25 check_if_nonempty('q2/a/julia.py'),26 check_if_nonempty('q2/a/bigbrother.py'),27 check_if_nonempty('q2/a/q2a1.txt'),28 check_if_nonempty('q2/a/q2a2.txt'),29 check_if_nonempty('q2/b/winston.py'),30 check_if_nonempty('q2/b/julia.py'),31 check_if_nonempty('q2/b/bigbrother.py'),32 check_if_nonempty('q2/b/q2b1.txt'),33 check_if_nonempty('q2/b/q2b2.txt'),34 ]):35 print('smoketest seems cool')36if __name__ == '__main__':...
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!!