def before(test): test.elapsed = 1.23456789 test.result = mock.Mock() test.test = mock.Mock() test.exc_info = fake_exc_info() test.stream = mock.Mock() test.formatter = result.DotsFormatter(test.stream)
def before(test): test.describes = mock.Mock(__name__="DescribedThing") test.it = describe(test.describes)
test.it = describe(test.describes) with it("returns the described object's name as its str") as test: test.assertEqual(str(test.it), test.it.describes.__name__) with it("shows its name and examples as its repr") as test: test.assertEqual( repr(test.it), "<{0.__class__.__name__} examples={0.examples}>".format(test.it), ) with it("sets the described object") as test: test.assertEqual(test.it.describes, test.describes) with it("passes along failureException to Examples") as test: test.it.failureException = mock.Mock() test.assertEqual( test.it("Example").failureException, test.it.failureException) with it("leaves the default failureException alone") as test: test.assertIsNone(test.it.failureException) test.assertIsNotNone(test.it("Example").failureException) with it("yields examples when iterating") as test: example, another = mock.Mock(), mock.Mock() test.it.add_example(example) test.it.add_example(another) test.assertEqual(list(test.it), [example, another]) with it("counts its examples") as test: test.assertEqual(test.it.countTestCases(), 0)
def before(test): test.exc_info = mock.Mock() test.formatter = mock.Mock() test.result = mock.Mock() test.test = mock.Mock() test.verbose = result.Verbose(test.formatter)
def before(test): test.formatter = mock.Mock() test.result = result.ExampleResult(test.formatter) test.test = mock.Mock() test.exc_info = fake_exc_info()
def before(test): test.result = mock.Mock() test.manager = ContextManager(test.result) test.context = test.manager.create_context("a test context")
test.assertEqual( test.formatter.timing(test.elapsed), "Finished in {:.6f} seconds.\n".format(test.elapsed), ) with it("formats tracebacks") as test: example = mock.MagicMock() example.__str__.return_value = "Example" traceback = "The\nTraceback\n" test.assertEqual( test.formatter.traceback(example, traceback), "\n".join([str(example), traceback]) ) with describe(result.DotsFormatter.show, Example=ExampleWithPatch) as it: @it.before def before(test): test.stream = StringIO() test.formatter = result.DotsFormatter(test.stream) with it("writes to stderr by default") as test: test.assertEqual(result.DotsFormatter().stream, sys.stderr) with it("writes and flushes") as test: test.stream.flush = mock.Mock() test.formatter.show("hello\n") test.assertEqual(test.stream.getvalue(), "hello\n") test.assertTrue(test.stream.flush.called)
def before(test): test.manager = mock.Mock() test.context = Context("a test context", test.manager)
mock.call("quux", "baz/quux"), ]) with it("loads paths") as test: test.isdir.return_value = False load.load_from_path(test.path) test.load_source.assert_called_once_with("bar", test.path) with describe(load.filter_specs, Example=ExampleWithPatch) as it: with it("filters out only specs") as test: files = ["a.py", "dir/b.py", "dir/c_spec.py", "d_spec.py"] specs = load.filter_specs(files) test.assertEqual(specs, ["dir/c_spec.py", "d_spec.py"]) with describe(load.discover, Example=ExampleWithPatch) as it: with it("discovers specs") as test: subdirs = mock.Mock() files, more_files = ["one"], ["two", "three"] tree = [("dir", subdirs, files), ("dir/child", subdirs, more_files)] walk = test.patchObject(load.os, "walk", return_value=tree) no_filter = mock.Mock(side_effect=lambda paths: paths) specs = list(load.discover("a/path", filter_specs=no_filter)) expected = ["dir/one", "dir/child/two", "dir/child/three"] test.assertEqual(specs, expected) test.assertTrue(no_filter.called) walk.assert_called_once_with("a/path")
def before(test): test.patchObject(ivoire, "current_result", None) test.config = mock.Mock(verbose=False, color=False)
def before(test): test.config = mock.Mock(specs=[]) test.load_by_name = test.patchObject(run, "load_by_name") test.result = test.patch("ivoire.current_result", failfast=False) test.setup = test.patchObject(run, "setup") test.exit = test.patchObject(run.sys, "exit")
def before(test): test.patchObject(run, "transform_possible", True) test.ExampleLoader = test.patchObject(run, "ExampleLoader") test.config = mock.Mock(runner="runner", specs=["a/spec.py"], args=[]) test.run_path = test.patchObject(run.runpy, "run_path")
run.run(test.config) test.setup.assert_called_once_with(test.config) with it("sets failfast on the result") as test: test.assertFalse(test.result.failfast) test.config.exitfirst = True run.run(test.config) test.assertTrue(test.result.failfast) with it("starts and stops a test run") as test: run.run(test.config) test.result.startTestRun.assert_called_once_with() test.result.stopTestRun.assert_called_once_with() with it("loads specs") as test: test.config.specs = [mock.Mock(), mock.Mock(), mock.Mock()] run.run(test.config) test.assertEqual( test.load_by_name.mock_calls, [mock.call(spec) for spec in test.config.specs], ) with it("succeeds with status code 0") as test: test.result.wasSuccessful.return_value = True run.run(test.config) test.exit.assert_called_once_with(0) with it("fails with status code 1") as test: test.result.wasSuccessful.return_value = False run.run(test.config) test.exit.assert_called_once_with(1)
mock.call("baz", "bar/baz"), mock.call("quux", "baz/quux"), ]) with it("loads paths") as test: test.isdir.return_value = False load.load_from_path(test.path) test.load_source.assert_called_once_with("bar", test.path) with describe(load.filter_specs, Example=ExampleWithPatch) as it: with it("filters out only specs") as test: files = ["a.py", "dir/b.py", "dir/c_spec.py", "d_spec.py"] specs = load.filter_specs(files) test.assertEqual(specs, ["dir/c_spec.py", "d_spec.py"]) with describe(load.discover, Example=ExampleWithPatch) as it: with it("discovers specs") as test: subdirs = mock.Mock() files, more_files = [mock.Mock()], [mock.Mock(), mock.Mock()] tree = [("dir", subdirs, files), ("dir/child", subdirs, more_files)] walk = test.patchObject(load.os, "walk", return_value=tree) no_filter = mock.Mock(side_effect=lambda paths: paths) specs = list(load.discover("a/path", filter_specs=no_filter)) test.assertEqual(specs, files + more_files) test.assertTrue(no_filter.called) walk.assert_called_once_with("a/path")
def before(test): test.name = "the name of the Example" test.example_group = mock.Mock() test.example = Example(test.name, test.example_group)
test.name = "the name of the Example" test.example_group = mock.Mock() test.example = Example(test.name, test.example_group) with it("shows its name as its str") as test: test.assertEqual(str(test.example), test.name) with it("shows its class and name in its repr") as test: test.assertEqual( repr(test.example), "<{0.__class__.__name__}: {0}>".format(test.example), ) with it("knows its group") as test: test.assertEqual(test.example.group, test.example_group) with it("prevents group from being accidentally set") as test: with test.assertRaises(AttributeError): test.example.group = 12 with it("has the same hash for the same name and group") as test: same = Example(test.name, test.example_group) test.assertEqual(hash(test.example), hash(same)) with it("has a different hash for other names and groups") as test: other = Example(test.name + " something else", test.example_group) another = Example(test.name, mock.Mock()) test.assertNotEqual(hash(test.example), hash(other)) test.assertNotEqual(hash(test.example), hash(another))