Best Python code snippet using Testify_python
test_program_test.py
Source:test_program_test.py  
1import re2import subprocess3import sys4import mock5from testify import setup_teardown, TestCase, test_program6from testify.assertions import assert_equal, assert_raises, assert_in7from optparse import OptionParser8class OptionParserErrorException(Exception):9    pass10class ParseTestRunnerCommandLineArgsTest(TestCase):11    @setup_teardown12    def patch_OptionParser_error(self):13        def new_error(*args, **kwargs):14            raise OptionParserErrorException(*args, **kwargs)15        with mock.patch.object(OptionParser, 'error', side_effect=new_error):16            yield17    def test__parse_test_runner_command_line_module_method_overrides_empty_input(self):18        """Make sure _parse_test_runner_command_line_module_method_overrides19        returns something sensible if you pass it an empty list of arguments.20        """21        assert_equal(test_program._parse_test_runner_command_line_module_method_overrides([]), (None, {}))22    def test_parse_test_runner_command_line_args_rerun_test_file(self):23        """Make sure that when --rerun-test-file is passed,24        parse_test_runner_command_line_args doesn't complain about a missing25        test path.26        """27        test_program.parse_test_runner_command_line_args([], ['--rerun-test-file', '-'])28    def test_parse_test_runner_command_line_args_replay_json_inline(self):29        """Make sure that when --replay-json-inline is passed,30        parse_test_runner_command_line_args doesn't complain about a missing31        test path.32        """33        test_program.parse_test_runner_command_line_args([], ['--replay-json-inline', '{something that obviously isnt json}'])34    def test_parse_test_runner_command_line_args_replay_json(self):35        """Make sure that when --replay-json-inline is passed,36        parse_test_runner_command_line_args doesn't complain about a missing37        test path.38        """39        test_program.parse_test_runner_command_line_args([], ['--replay-json', 'somejsonfile.txt'])40    def test_parse_test_runner_command_line_args_no_test_path(self):41        """Make sure that if no options and no arguments are passed,42        parse_test_runner_command_line_args DOES complain about a missing test43        path.44        """45        with assert_raises(OptionParserErrorException):46            test_program.parse_test_runner_command_line_args([], [])47def test_call(command):48    proc = subprocess.Popen(command, stdout=subprocess.PIPE)49    stdout, stderr = proc.communicate()50    if proc.returncode:51        raise subprocess.CalledProcessError(proc.returncode, command)52    return stdout.strip().decode('UTF-8')53class TestifyRunAcceptanceTestCase(TestCase):54    expected_list = (55        'testing_suite.example_test ExampleTestCase.test_one\n'56        'testing_suite.example_test ExampleTestCase.test_two\n'57        'testing_suite.example_test SecondTestCase.test_one'58    )59    expected_tests = 'PASSED.  3 tests'60    def test_help(self):61        output = test_call([sys.executable, '-m', 'testify.test_program', '--help'])62        assert_in('Usage:', output)63    def test_run_testify_from_bin_list_tests(self):64        output = test_call(['bin/testify', '--list-tests', 'testing_suite'])65        assert_equal(output, self.expected_list)66    def test_run_testify_as_module_list_tests(self):67        output = test_call([68            sys.executable, '-m', 'testify.test_program',69            '--list-tests', 'testing_suite'])70        assert_equal(output, self.expected_list)71    def test_list_tests_json(self):72        output = test_call([73            sys.executable, '-m', 'testify.test_program',74            '--list-tests', 'testing_suite',75            '--list-tests-format', 'json',76        ])77        assert_equal(output, '''\78{"suites": [], "test": "testing_suite.example_test ExampleTestCase.test_one"}79{"suites": [], "test": "testing_suite.example_test ExampleTestCase.test_two"}80{"suites": [], "test": "testing_suite.example_test SecondTestCase.test_one"}''')81    def test_list_tests_json_suites(self):82        output = test_call([83            sys.executable, '-m', 'testify.test_program',84            '--list-tests', 'test.test_suites_test',85            '--list-tests-format', 'json',86        ])87        assert_equal(output, '''\88{"suites": ["class-level-suite", "disabled", "example", "module-level"], "test": "test.test_suites_test ListSuitesTestCase.test_also_disabled"}89{"suites": ["class-level-suite", "crazy", "disabled", "example", "module-level"], "test": "test.test_suites_test ListSuitesTestCase.test_disabled"}90{"suites": ["class-level-suite", "example", "module-level"], "test": "test.test_suites_test ListSuitesTestCase.<lambda>"}91{"suites": ["assertion", "class-level-suite", "example", "module-level"], "test": "test.test_suites_test ListSuitesTestCase.test_list_suites"}92{"suites": ["class-level-suite", "example", "module-level"], "test": "test.test_suites_test ListSuitesTestCase.test_not_disabled"}93{"suites": ["class-level-suite", "disabled", "example", "module-level"], "test": "test.test_suites_test TestifiedListSuitesUnittestCase.test_also_disabled"}94{"suites": ["class-level-suite", "crazy", "disabled", "example", "module-level"], "test": "test.test_suites_test TestifiedListSuitesUnittestCase.test_disabled"}95{"suites": ["assertion", "class-level-suite", "example", "module-level"], "test": "test.test_suites_test TestifiedListSuitesUnittestCase.test_list_suites"}96{"suites": ["class-level-suite", "example", "module-level"], "test": "test.test_suites_test TestifiedListSuitesUnittestCase.test_not_disabled"}97{"suites": ["example", "module-level", "sub"], "test": "test.test_suites_test SubDecoratedTestCase.test_thing"}98{"suites": ["example", "module-level", "sub", "super"], "test": "test.test_suites_test SubTestCase.test_thing"}99{"suites": ["example", "module-level", "super"], "test": "test.test_suites_test SuperDecoratedTestCase.test_thing"}100{"suites": ["example", "module-level", "super"], "test": "test.test_suites_test SuperTestCase.test_thing"}101{"suites": ["module-level"], "test": "test.test_suites_test TestSuitesTestCase.test_subclass_suites_doesnt_affect_superclass_suites"}102{"suites": ["module-level"], "test": "test.test_suites_test TestSuitesTestCase.test_suite_decorator_overrides_parent"}''')  # noqa103    def assert_rerun_discovery(self, format):104        output = test_call([105            'sh', '-c', '''\106{python} -m testify.test_program --list-tests test.test_suites_test --list-tests-format {format} |107{python} -m testify.test_program -v --require-suite example --exclude-suite disabled --exclude-suite assertion --rerun-test-file -108'''.format(python=sys.executable, format=format)109        ])110        output = re.sub(r'\b[0-9.]+s\b', '${TIME}', output)111        assert_equal(output, '''\112test.test_suites_test ListSuitesTestCase.<lambda> ... ok in ${TIME}113test.test_suites_test ListSuitesTestCase.test_not_disabled ... ok in ${TIME}114test.test_suites_test TestifiedListSuitesUnittestCase.test_not_disabled ... ok in ${TIME}115test.test_suites_test SubDecoratedTestCase.test_thing ... ok in ${TIME}116test.test_suites_test SubTestCase.test_thing ... ok in ${TIME}117test.test_suites_test SuperDecoratedTestCase.test_thing ... ok in ${TIME}118test.test_suites_test SuperTestCase.test_thing ... ok in ${TIME}119PASSED.  7 tests / 6 cases: 7 passed, 0 failed.  (Total test time ${TIME})''')120    def test_rerun_discovery_txt(self):121        self.assert_rerun_discovery('txt')122    def test_rerun_discovery_json(self):123        self.assert_rerun_discovery('json')124    def test_run_testify_from_bin(self):125        output = test_call(['bin/testify', 'testing_suite', '-v'])126        assert_in(self.expected_tests, output)127    def test_run_testify_test_module(self):128        output = test_call([sys.executable, '-m', 'testing_suite.example_test', '-v'])129        assert_in(self.expected_tests, output)130    def test_run_testify_test_file(self):131        output = test_call([sys.executable, 'testing_suite/example_test.py', '-v'])132        assert_in(self.expected_tests, output)133    def test_run_testify_test_file_class(self):134        output = test_call([135            sys.executable, 'testing_suite/example_test.py', '-v',136            'ExampleTestCase'])137        assert_in('PASSED.  2 tests', output)138    def test_run_testify_test_file_class_and_method(self):139        output = test_call([140            sys.executable, 'testing_suite/example_test.py', '-v',141            'ExampleTestCase.test_one'])142        assert_in('PASSED.  1 test', output)143    def test_run_testify_with_failure(self):144        assert_raises(145            subprocess.CalledProcessError,146            test_call,147            [sys.executable, 'testing_suite/example_test.py', 'DoesNotExist'])148    def test_failure_on_interrupt(self):149        with assert_raises(subprocess.CalledProcessError):150            test_call([151                sys.executable, '-m', 'testify.test_program',152                'test.failing_test_interrupt'153            ])154    def test_rerun_with_failure_limit(self):155        proc = subprocess.Popen(156            (157                sys.executable, '-m', 'testify.test_program',158                '--rerun-test-file=/dev/stdin',159                '--failure-limit', '1',160            ),161            stdin=subprocess.PIPE,162            stdout=subprocess.PIPE,163        )164        stdout, _ = proc.communicate(165            b'test.fails_two_tests FailsTwoTests.test1\n'166            b'test.fails_two_tests FailsTwoTests.test2\n'167        )...test_suites_test.py
Source:test_suites_test.py  
...52    @suite('disabled', 'crazy', conditions=True)53    def test_disabled(self):54        True55    @suite('disabled', reason='blah')56    def test_also_disabled(self):57        True58    @suite('not-applied', conditions=False)59    def test_not_disabled(self):60        True61    @suite('assertion')62    def test_list_suites(self):63        # for suites affecting all of this class's tests64        num_tests = len(list(self.runnable_test_methods()))65        test_runner = TestRunner(type(self))66        assert_equal(sorted(test_runner.list_suites().items()), [67            ('assertion', '1 tests'),68            ('class-level-suite', '%d tests' % num_tests),69            ('crazy', '1 tests'),70            ('disabled', '2 tests'),...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!!
