Best Python code snippet using unittest-xml-reporting_python
test_doctest_example.py
Source: test_doctest_example.py
1# -*- coding: utf-8 -*-2from __future__ import print_function, division, absolute_import, unicode_literals3from xdoctest import doctest_example4from xdoctest import utils5from xdoctest import constants6from xdoctest import checker7def test_exit_test_exception():8 """9 pytest tests/test_doctest_example.py::test_exit_test_exception10 """11 string = utils.codeblock(12 '''13 >>> from xdoctest import ExitTestException14 >>> raise ExitTestException()15 >>> 0 / 0 # should never reach this16 217 ''')18 self = doctest_example.DocTest(docsrc=string)19 result = self.run(on_error='raise')20 assert result['passed']21def test_failed_assign_want():22 """23 pytest tests/test_doctest_example.py::test_exit_test_exception24 """25 string = utils.codeblock(26 '''27 >>> name = 'foo'28 'anything'29 ''')30 self = doctest_example.DocTest(docsrc=string)31 result = self.run(on_error='return', verbose=0)32 assert result['failed']33 fail_text = '\n'.join(self.repr_failure())34 assert 'Got nothing' in fail_text35def test_continue_ambiguity():36 """37 pytest tests/test_doctest_example.py::test_exit_test_exception38 """39 string = utils.codeblock(40 '''41 >>> class Lowerer(object):42 ... def __init__(self):43 ... self.cache = LRI()44 ...45 ... def lower(self, text):46 ... return text.lower()47 ...48 ''')49 self = doctest_example.DocTest(docsrc=string)50 result = self.run(on_error='return', verbose=3)51 assert result['passed']52def test_contination_want_ambiguity():53 """54 xdoctest ~/code/xdoctest/tests/test_doctest_example.py test_contination_want_ambiguity55 """56 string = utils.codeblock(57 '''58 >>> class Lowerer(object):59 ... def __init__(self):60 ... self.cache = LRI()61 ...62 ... def lower(self, text):63 ... return text.lower()64 ...65 ''')66 self = doctest_example.DocTest(docsrc=string)67 result = self.run(on_error='return', verbose=3)68 assert result['passed']69def test_multiline_list():70 """71 pytest tests/test_doctest_example.py::test_multiline_list72 """73 string = utils.codeblock(74 '''75 >>> x = [1, 2, 3,76 >>> 4, 5, 6]77 >>> print(len(x))78 679 ''')80 self = doctest_example.DocTest(docsrc=string)81 result = self.run(on_error='raise')82 assert result['passed']83def test_failure():84 string = utils.codeblock(85 '''86 >>> i = 087 >>> 0 / i88 289 ''')90 self = doctest_example.DocTest(docsrc=string, lineno=1000)91 self._parse()92 try:93 self.run(on_error='raise')94 except ZeroDivisionError:95 pass96 else:97 raise AssertionError('should have gotten zero division')98 result = self.run(on_error='return')99 assert not result['passed']100def test_format_src():101 """102 python tests/test_doctest_example.py test_format_src103 pytest tests/test_doctest_example.py::test_format_src -s -v104 """105 string = utils.codeblock(106 '''107 >>> i = 0108 >>> 0 / i109 2110 ''')111 string_with_lineno = utils.codeblock(112 '''113 1 >>> i = 0114 2 >>> 0 / i115 2116 ''').replace('!', ' ')117 self = doctest_example.DocTest(docsrc=string)118 self._parse()119 assert self.format_src(colored=0, linenos=1) == string_with_lineno120 assert self.format_src(colored=0, linenos=0) == string121 assert utils.strip_ansi(self.format_src(colored=1, linenos=1)) == string_with_lineno122 assert utils.strip_ansi(self.format_src(colored=1, linenos=0)) == string123def test_eval_expr_capture():124 """125 pytest tests/test_doctest_example.py::test_eval_expr_capture -s126 """127 docsrc = utils.codeblock(128 '''129 >>> x = 3130 >>> y = x + 2131 >>> y + 2132 2133 ''')134 self = doctest_example.DocTest(docsrc=docsrc)135 self._parse()136 p1, p2 = self._parts137 # test_globals = {}138 # code1 = compile(p1.source, '<string>', 'exec')139 # exec(code1, test_globals)140 # code2 = compile(p2.source, '<string>', 'eval')141 # result = eval(code2, test_globals)142 try:143 self.run()144 except Exception as ex:145 assert hasattr(ex, 'output_difference')146 msg = ex.output_difference(colored=False)147 assert msg == utils.codeblock(148 '''149 Expected:150 2151 Got:152 7153 ''')154def test_run_multi_want():155 docsrc = utils.codeblock(156 '''157 >>> x = 2158 >>> x159 2160 >>> 'string'161 'string'162 >>> print('string')163 string164 ''')165 self = doctest_example.DocTest(docsrc=docsrc)166 self.run()167 result = self.run()168 assert result['passed']169 assert list(self.logged_stdout.values()) == ['', '', '', 'string\n']170 assert list(self.logged_evals.values()) == [constants.NOT_EVALED, 2, 'string', None]171def test_comment():172 docsrc = utils.codeblock(173 '''174 >>> # foobar175 ''')176 self = doctest_example.DocTest(docsrc=docsrc)177 self._parse()178 assert len(self._parts) == 1179 self.run(verbose=0)180 docsrc = utils.codeblock(181 '''182 >>> # foobar183 >>> # bazbiz184 ''')185 self = doctest_example.DocTest(docsrc=docsrc)186 self._parse()187 assert len(self._parts) == 1188 self.run(verbose=0)189 docsrc = utils.codeblock(190 '''191 >>> # foobar192 >>> x = 0193 >>> x / 0194 >>> # bazbiz195 ''')196 self = doctest_example.DocTest(docsrc=docsrc, lineno=1)197 self._parse()198 assert len(self._parts) == 1199 result = self.run(on_error='return', verbose=0)200 assert not result['passed']201 assert self.failed_lineno() == 3202def test_want_error_msg():203 """204 python tests/test_doctest_example.py test_want_error_msg205 pytest tests/test_doctest_example.py::test_want_error_msg206 """207 string = utils.codeblock(208 '''209 >>> raise Exception('everything is fine')210 Traceback (most recent call last):211 Exception: everything is fine212 ''')213 self = doctest_example.DocTest(docsrc=string)214 result = self.run(on_error='raise')215 assert result['passed']216def test_want_error_msg_failure():217 """218 python tests/test_doctest_example.py test_want_error_msg_failure219 pytest tests/test_doctest_example.py::test_want_error_msg_failure220 """221 string = utils.codeblock(222 '''223 >>> raise Exception('everything is NOT fine')224 Traceback (most recent call last):225 Exception: everything is fine226 ''')227 self = doctest_example.DocTest(docsrc=string)228 import pytest229 with pytest.raises(checker.GotWantException):230 self.run(on_error='raise')231if __name__ == '__main__':232 """233 CommandLine:234 export PYTHONPATH=$PYTHONPATH:/home/joncrall/code/xdoctest/tests235 python ~/code/xdoctest/tests/test_doctest_example.py236 xdoctest ~/code/xdoctest/tests/test_doctest_example.py zero-args237 """238 import xdoctest...
ex4_test.py
Source: ex4_test.py
...82 self.assertEqual(duplicate(x), [x, x])83 @given(nested_lists())84 def test_twice_as_many_ints(self, x):85 self.assertEqual(num_ints(duplicate(x)), 2 * num_ints(x))86 def test_doctest_example(self):87 nested_list = [1, [2, 3]]88 expected = [1, 1, [2, 2, 3, 3]]89 self.assertEqual(duplicate(nested_list), expected)90class AddOneTest(unittest.TestCase):91 @given(lists(integers()))92 def test_list_of_ints(self, nested_list):93 copy = nested_list[:] # create a copy of the input94 add_one(copy)95 for i in range(len(nested_list)):96 self.assertEqual(copy[i], nested_list[i] + 1)97 def test_doctest_example(self):98 nested_list = [1, [2, 3], [[[5]]]]99 expected = [2, [3, 4], [[[6]]]]100 add_one(nested_list)101 self.assertEqual(nested_list, expected)102 @given(nested_lists())103 def test_same_number_of_lists_and_ints(self, x):104 old_num_lists = num_lists(x)105 old_num_ints = num_ints(x)106 add_one(x)107 self.assertEqual(num_lists(x), old_num_lists)108 self.assertEqual(num_ints(x), old_num_ints)109##############################################################################110# Task 2: Family trees111##############################################################################...
Check out the latest blogs from LambdaTest on this topic:
Have you ever visited a website that only has plain text and images? Most probably, no. It’s because such websites do not exist now. But there was a time when websites only had plain text and images with almost no styling. For the longest time, websites did not focus on user experience. For instance, this is how eBay’s homepage looked in 1999.
The key to successful test automation is to focus on tasks that maximize the return on investment (ROI), ensuring that you are automating the right tests and automating them in the right way. This is where test automation strategies come into play.
Unit testing is typically software testing within the developer domain. As the QA role expands in DevOps, QAOps, DesignOps, or within an Agile team, QA testers often find themselves creating unit tests. QA testers may create unit tests within the code using a specified unit testing tool, or independently using a variety of methods.
“Test frequently and early.” If you’ve been following my testing agenda, you’re probably sick of hearing me repeat that. However, it is making sense that if your tests detect an issue soon after it occurs, it will be easier to resolve. This is one of the guiding concepts that makes continuous integration such an effective method. I’ve encountered several teams who have a lot of automated tests but don’t use them as part of a continuous integration approach. There are frequently various reasons why the team believes these tests cannot be used with continuous integration. Perhaps the tests take too long to run, or they are not dependable enough to provide correct results on their own, necessitating human interpretation.
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!!