def test_args_optional(self): with captured(out(), err()) as (stdout, stderr): with mock_args([]): TestCommand.main() self.assertIn('opt=None', stdout.getvalue()) with captured(out(), err()) as (stdout, stderr): with mock_args(['--opt', 'OPT']): TestCommand.main() self.assertIn('opt=OPT', stdout.getvalue())
def test_get_version(self): with abduct.captured(abduct.out(), abduct.err()) as (stdout, stderr): with __main__.App(argv=['-v']) as app: with self.assertRaises(SystemExit): app.run() self.assertEqual(stdout.getvalue().strip(), wc_sim.__version__) self.assertEqual(stderr.getvalue(), '') with abduct.captured(abduct.out(), abduct.err()) as (stdout, stderr): with __main__.App(argv=['--version']) as app: with self.assertRaises(SystemExit): app.run() self.assertEqual(stdout.getvalue().strip(), wc_sim.__version__) self.assertEqual(stderr.getvalue(), '')
def test_args_logging_level(self): with captured(out(), err()) as (stdout, stderr): with mock_args([]): TestCommand.main() self.assertIn('logging_level=%s' % logging.WARNING, stdout.getvalue()) with captured(out(), err()) as (stdout, stderr): with mock_args(['--verbose']): TestCommand.main() self.assertIn('logging_level=%s' % logging.INFO, stdout.getvalue()) with captured(out(), err()) as (stdout, stderr): with mock_args(['--debug']): TestCommand.main() self.assertIn('logging_level=%s' % logging.DEBUG, stdout.getvalue())
def test_captured_stderr(stdout, stderr): with captured(err()) as stderr_cap: write() eq_(stderr_cap.getvalue(), 'stderr') eq_(stdout.getvalue(), 'stdout') eq_(stderr.getvalue(), '')
def test_capture_stdout_and_stderr(stdout, stderr): @captured(out(), err()) def run(): write() run() eq_(stdout.getvalue(), '') eq_(stderr.getvalue(), '')
def test_captured_stdout_and_stderr(stdout, stderr): with captured(out(), err()) as (stdout_cap, stderr_cap): write() eq_(stdout_cap.getvalue(), 'stdout') eq_(stderr_cap.getvalue(), 'stderr') eq_(stdout.getvalue(), '') eq_(stderr.getvalue(), '')
def test_args_missing(self): with captured(out(), err()) as (stdout, stderr): with mock_args([]): with self.assertRaises(SystemExit): RequiredArgCommand.main() # Py2 and Py3 argparse have different messages self.assertIn('error: ', stderr.getvalue()) self.assertIn('-r/--req', stderr.getvalue()) self.assertIn('required', stderr.getvalue())
def run_command(additional_args): httpretty_register() args = ['-d', 'DOMAIN', '-n', 'NAME', '-v', 'VERSION', '-i', 'ID'] args += additional_args with captured(out(), err()) as (stdout, stderr): with mock_args(args): Command.main() return stdout, stderr
def run_command(additional_args): httpretty_register() args = ["-d", "DOMAIN"] args += additional_args with captured(out(), err()) as (stdout, stderr): with mock_args(args): Command.main() return stdout, stderr
def test_args_logging_config(self): with InTempDir(): with open('logging.conf', 'w') as fd: fd.write(LOGGING_CONFIG_FILE_DATA) args = ['--logging-config', 'logging.conf'] with captured(out(), err()) as (stdout, stderr): with mock_args(args): TestCommand.main() self.assertIn('EndOfOutput', stdout.getvalue())
def test_raw_cli(self): with mock.patch('sys.argv', ['wc-sim', '--help']): with self.assertRaises(SystemExit) as context: __main__.main() self.assertRegex(context.Exception, 'usage: wc-sim') with mock.patch('sys.argv', ['wc-sim']): with abduct.captured(abduct.out(), abduct.err()) as (stdout, stderr): __main__.main() self.assertRegex(stdout.getvalue().strip(), 'usage: wc-sim') self.assertEqual(stderr.getvalue(), '')
def test_nominal(self): args = ["--name", "DOMAIN", "--retention-days", "10"] headers = {"x-amzn-RequestId": "d68969c7-3f0d-11e1-9b11-7182192d0b57"} httpretty.register_uri( httpretty.POST, "https://swf.us-east-1.amazonaws.com/", content_type="application/json", adding_headers=headers, body="", ) with captured(out(), err()) as (stdout, stderr): with mock_args(args): Command.main() request = httpretty.last_request() self.assertEqual(request.headers.get("x-amz-target"), "SimpleWorkflowService.RegisterDomain") expected = {"name": "DOMAIN", "workflowExecutionRetentionPeriodInDays": "10"} self.assertEqual(json.loads(request.parsed_body), expected) self.assertIn("Success.", stdout.getvalue())
def test_quality_as_word_ok(quality_word, expected_itag): ''' Test that qualities are correctly parsed into their equivalent itags. A unit test for this is not enough, as this involves configuration of the argument parser, and determination of output from the program. This is essentially our "everything is generally ok" end to end test. ''' # expected_raw is a sequence of (itag, url) pairs. Since we're specifically # looking for the itag corresponding to to the quality word, we convert # these tuples to a dict and pull out the URL for the expected itag. expected_raw = _test_utils.read_fixture('files/success_output') expected_url = dict(json.loads(expected_raw))[expected_itag] fake_api_output = _test_utils.read_fixture('files/success_input', 'rb') _test_utils.mock_get_video_info_api_response(fake_api_output) with captured(out(), err()) as (stdout, stderr): yturl.main(['-q', quality_word, _test_utils.FAKE_URL], ) eq(stderr.getvalue(), 'Using itag %d.\n' % expected_itag) eq(stdout.getvalue(), expected_url + '\n')
def assert_scenario(stdout, stderr, scenario): context_out = 'stdout' if scenario.real_output_in_context else '' context_err = 'stderr' if scenario.real_output_in_context else '' except_out = 'stdout' if scenario.real_output_in_except else '' except_err = 'stderr' if scenario.real_output_in_except else '' o = out(release_on_exception=scenario.release, tee=scenario.tee) e = err(release_on_exception=scenario.release, tee=scenario.tee) try: with captured(o, e) as (stdout_cap, stderr_cap): write() eq_(stdout_cap.getvalue(), 'stdout') eq_(stderr_cap.getvalue(), 'stderr') eq_(stdout.getvalue(), context_out) eq_(stderr.getvalue(), context_err) raise MyCustomException() except MyCustomException: eq_(stdout.getvalue(), except_out) eq_(stderr.getvalue(), except_err)
def test_args_help(self): with captured(out(), err()) as (stdout, stderr): with mock_args(['--help']): with self.assertRaises(SystemExit): TestCommand.main() self.assertIn(TestCommand.description, stdout.getvalue())
def test_keyboard_interrupt(self): with captured(out(), err()) as (stdout, stderr): with mock_args([]): with self.assertRaises(SystemExit) as exc: KeyboardInterruptCommand.main() self.assertEqual(str(exc.exception), '1')