Best Python code snippet using Testify_python
test_formatters.py
Source:test_formatters.py
...85def test_for_type():86 f = PlainTextFormatter()87 88 # initial return, None89 nt.assert_is(f.for_type(C, foo_printer), None)90 # no func queries91 nt.assert_is(f.for_type(C), foo_printer)92 # shouldn't change anything93 nt.assert_is(f.for_type(C), foo_printer)94 # None should do the same95 nt.assert_is(f.for_type(C, None), foo_printer)96 nt.assert_is(f.for_type(C, None), foo_printer)97def test_for_type_string():98 f = PlainTextFormatter()99 100 mod = C.__module__101 102 type_str = '%s.%s' % (C.__module__, 'C')103 104 # initial return, None105 nt.assert_is(f.for_type(type_str, foo_printer), None)106 # no func queries107 nt.assert_is(f.for_type(type_str), foo_printer)108 nt.assert_in(_mod_name_key(C), f.deferred_printers)109 nt.assert_is(f.for_type(C), foo_printer)110 nt.assert_not_in(_mod_name_key(C), f.deferred_printers)111 nt.assert_in(C, f.type_printers)112def test_for_type_by_name():113 f = PlainTextFormatter()114 115 mod = C.__module__116 117 # initial return, None118 nt.assert_is(f.for_type_by_name(mod, 'C', foo_printer), None)119 # no func queries120 nt.assert_is(f.for_type_by_name(mod, 'C'), foo_printer)121 # shouldn't change anything122 nt.assert_is(f.for_type_by_name(mod, 'C'), foo_printer)123 # None should do the same124 nt.assert_is(f.for_type_by_name(mod, 'C', None), foo_printer)125 nt.assert_is(f.for_type_by_name(mod, 'C', None), foo_printer)126def test_lookup():127 f = PlainTextFormatter()128 129 f.for_type(C, foo_printer)130 nt.assert_is(f.lookup(C()), foo_printer)131 with nt.assert_raises(KeyError):132 f.lookup(A())133def test_lookup_string():134 f = PlainTextFormatter()135 type_str = '%s.%s' % (C.__module__, 'C')136 137 f.for_type(type_str, foo_printer)138 nt.assert_is(f.lookup(C()), foo_printer)139 # should move from deferred to imported dict140 nt.assert_not_in(_mod_name_key(C), f.deferred_printers)141 nt.assert_in(C, f.type_printers)142def test_lookup_by_type():143 f = PlainTextFormatter()144 f.for_type(C, foo_printer)145 nt.assert_is(f.lookup_by_type(C), foo_printer)146 type_str = '%s.%s' % (C.__module__, 'C')147 with nt.assert_raises(KeyError):148 f.lookup_by_type(A)149def test_lookup_by_type_string():150 f = PlainTextFormatter()151 type_str = '%s.%s' % (C.__module__, 'C')152 f.for_type(type_str, foo_printer)153 154 # verify insertion155 nt.assert_in(_mod_name_key(C), f.deferred_printers)156 nt.assert_not_in(C, f.type_printers)157 158 nt.assert_is(f.lookup_by_type(type_str), foo_printer)159 # lookup by string doesn't cause import160 nt.assert_in(_mod_name_key(C), f.deferred_printers)161 nt.assert_not_in(C, f.type_printers)162 163 nt.assert_is(f.lookup_by_type(C), foo_printer)164 # should move from deferred to imported dict165 nt.assert_not_in(_mod_name_key(C), f.deferred_printers)166 nt.assert_in(C, f.type_printers)167def test_in_formatter():168 f = PlainTextFormatter()169 f.for_type(C, foo_printer)170 type_str = '%s.%s' % (C.__module__, 'C')171 nt.assert_in(C, f)172 nt.assert_in(type_str, f)173def test_string_in_formatter():174 f = PlainTextFormatter()175 type_str = '%s.%s' % (C.__module__, 'C')176 f.for_type(type_str, foo_printer)177 nt.assert_in(type_str, f)178 nt.assert_in(C, f)179def test_pop():180 f = PlainTextFormatter()181 f.for_type(C, foo_printer)182 nt.assert_is(f.lookup_by_type(C), foo_printer)183 nt.assert_is(f.pop(C, None), foo_printer)184 f.for_type(C, foo_printer)185 nt.assert_is(f.pop(C), foo_printer)186 with nt.assert_raises(KeyError):187 f.lookup_by_type(C)188 with nt.assert_raises(KeyError):189 f.pop(C)190 with nt.assert_raises(KeyError):191 f.pop(A)192 nt.assert_is(f.pop(A, None), None)193def test_pop_string():194 f = PlainTextFormatter()195 type_str = '%s.%s' % (C.__module__, 'C')196 197 with nt.assert_raises(KeyError):198 f.pop(type_str)199 200 f.for_type(type_str, foo_printer)201 f.pop(type_str)202 with nt.assert_raises(KeyError):203 f.lookup_by_type(C)204 with nt.assert_raises(KeyError):205 f.pop(type_str)206 f.for_type(C, foo_printer)207 nt.assert_is(f.pop(type_str, None), foo_printer)208 with nt.assert_raises(KeyError):209 f.lookup_by_type(C)210 with nt.assert_raises(KeyError):211 f.pop(type_str)212 nt.assert_is(f.pop(type_str, None), None)213 214def test_error_method():215 f = HTMLFormatter()216 class BadHTML(object):217 def _repr_html_(self):218 raise ValueError("Bad HTML")219 bad = BadHTML()220 with capture_output() as captured:221 result = f(bad)222 nt.assert_is(result, None)223 nt.assert_in("Traceback", captured.stdout)224 nt.assert_in("Bad HTML", captured.stdout)225 nt.assert_in("_repr_html_", captured.stdout)226def test_nowarn_notimplemented():227 f = HTMLFormatter()228 class HTMLNotImplemented(object):229 def _repr_html_(self):230 raise NotImplementedError231 h = HTMLNotImplemented()232 with capture_output() as captured:233 result = f(h)234 nt.assert_is(result, None)235 nt.assert_equal("", captured.stderr)236 nt.assert_equal("", captured.stdout)237def test_warn_error_for_type():238 f = HTMLFormatter()239 f.for_type(int, lambda i: name_error)240 with capture_output() as captured:241 result = f(5)242 nt.assert_is(result, None)243 nt.assert_in("Traceback", captured.stdout)244 nt.assert_in("NameError", captured.stdout)245 nt.assert_in("name_error", captured.stdout)246def test_error_pretty_method():247 f = PlainTextFormatter()248 class BadPretty(object):249 def _repr_pretty_(self):250 return "hello"251 bad = BadPretty()252 with capture_output() as captured:253 result = f(bad)254 nt.assert_is(result, None)255 nt.assert_in("Traceback", captured.stdout)256 nt.assert_in("_repr_pretty_", captured.stdout)257 nt.assert_in("given", captured.stdout)258 nt.assert_in("argument", captured.stdout)259def test_bad_repr_traceback():260 f = PlainTextFormatter()261 bad = BadRepr()262 with capture_output() as captured:263 result = f(bad)264 # catches error, returns None265 nt.assert_is(result, None)266 nt.assert_in("Traceback", captured.stdout)267 nt.assert_in("__repr__", captured.stdout)268 nt.assert_in("ValueError", captured.stdout)269class MakePDF(object):270 def _repr_pdf_(self):271 return 'PDF'272def test_pdf_formatter():273 pdf = MakePDF()274 f = PDFFormatter()275 nt.assert_equal(f(pdf), 'PDF')276def test_print_method_bound():277 f = HTMLFormatter()278 class MyHTML(object):279 def _repr_html_(self):280 return "hello"281 with capture_output() as captured:282 result = f(MyHTML)283 nt.assert_is(result, None)284 nt.assert_not_in("FormatterWarning", captured.stderr)285 with capture_output() as captured:286 result = f(MyHTML())287 nt.assert_equal(result, "hello")288 nt.assert_equal(captured.stderr, "")289def test_print_method_weird():290 class TextMagicHat(object):291 def __getattr__(self, key):292 return key293 f = HTMLFormatter()294 295 text_hat = TextMagicHat()296 nt.assert_equal(text_hat._repr_html_, '_repr_html_')297 with capture_output() as captured:298 result = f(text_hat)299 300 nt.assert_is(result, None)301 nt.assert_not_in("FormatterWarning", captured.stderr)302 class CallableMagicHat(object):303 def __getattr__(self, key):304 return lambda : key305 306 call_hat = CallableMagicHat()307 with capture_output() as captured:308 result = f(call_hat)309 310 nt.assert_equal(result, None)311 class BadReprArgs(object):312 def _repr_html_(self, extra, args):313 return "html"314 315 bad = BadReprArgs()316 with capture_output() as captured:317 result = f(bad)318 319 nt.assert_is(result, None)320 nt.assert_not_in("FormatterWarning", captured.stderr)321def test_format_config():322 """config objects don't pretend to support fancy reprs with lazy attrs"""323 f = HTMLFormatter()324 cfg = Config()325 with capture_output() as captured:326 result = f(cfg)327 nt.assert_is(result, None)328 nt.assert_equal(captured.stderr, "")329 with capture_output() as captured:330 result = f(Config)331 nt.assert_is(result, None)332 nt.assert_equal(captured.stderr, "")333def test_pretty_max_seq_length():334 f = PlainTextFormatter(max_seq_length=1)335 lis = list(range(3))336 text = f(lis)337 nt.assert_equal(text, '[0, ...]')338 f.max_seq_length = 0339 text = f(lis)340 nt.assert_equal(text, '[0, 1, 2]')341 text = f(list(range(1024)))342 lines = text.splitlines()343 nt.assert_equal(len(lines), 1024)344def test_ipython_display_formatter():345 """Objects with _ipython_display_ defined bypass other formatters"""...
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!!