Module: testing.tools
Generic testing tools.
3 Classes
- class IPython.testing.tools.TempFileMixin(methodName='runTest')
Bases:
TestCaseUtility class to create temporary Python/IPython files.
Meant as a mixin class for test cases.
- tearDown()
Hook method for deconstructing the test fixture after testing it.
- class IPython.testing.tools.AssertPrints(s: str, channel: str = 'stdout', suppress: bool = True)
Bases:
objectContext manager for testing that code prints certain text.
Examples
>>> with AssertPrints("abc", suppress=False): ... print("abcd") ... print("def") ... abcd def
- class IPython.testing.tools.AssertNotPrints(s: str, channel: str = 'stdout', suppress: bool = True)
Bases:
AssertPrintsContext manager for checking that certain output isn’t produced.
Counterpart of AssertPrints
11 Functions
- IPython.testing.tools.full_path(startPath: str, files: list[str]) list[str]
Make full paths for all the listed files, based on startPath.
Only the base part of startPath is kept, since this routine is typically used with a script’s
__file__variable as startPath. The base of startPath is then prepended to all the listed files, forming the output list.- Parameters:
startPath (string) – Initial path to use as the base for the results. This path is split using os.path.split() and only its first component is kept.
files (list) – One or more files.
Examples
>>> full_path('/foo/bar.py',['a.txt','b.txt']) ['/foo/a.txt', '/foo/b.txt']
>>> full_path('/foo',['a.txt','b.txt']) ['/a.txt', '/b.txt']
- IPython.testing.tools.parse_test_output(txt: str) Tuple[int, int]
Parse the output of a test run and return errors, failures.
- Parameters:
txt (str) –
Text output of a test run, assumed to contain a line of one of the following forms:
'FAILED (errors=1)' 'FAILED (failures=1)' 'FAILED (errors=1, failures=1)'
- Returns:
number of errors and failures.
- Return type:
nerr, nfail
- IPython.testing.tools.default_argv() List[str]
Return a valid default argv for creating testing instances of ipython
- IPython.testing.tools.default_config() Config
Return a config object with good defaults for testing.
- IPython.testing.tools.get_ipython_cmd(as_string: bool = False) List[str]
Return appropriate IPython command line name. By default, this will return a list that can be used with subprocess.Popen, for example, but passing
as_string=Trueallows for returning the IPython command as a string.- Parameters:
as_string (bool) – Flag to allow to return the command as a string.
- IPython.testing.tools.ipexec(fname: str, options: List[str] | None = None, commands: Tuple[str, ...] = ()) Tuple[str, str]
Utility to call ‘ipython filename’.
Starts IPython with a minimal and safe configuration to make startup as fast as possible.
Note that this starts IPython in a subprocess!
- IPython.testing.tools.ipexec_validate(fname: str, expected_out: str, expected_err: str = '', options: List[str] | None = None, commands: Tuple[str, ...] = ())
Utility to call ‘ipython filename’ and validate output/error.
This function raises an AssertionError if the validation fails.
Note that this starts IPython in a subprocess!
- Parameters:
- Return type:
None
- IPython.testing.tools.make_tempfile(name)
Create an empty, named, temporary file for the duration of the context.
- IPython.testing.tools.fake_input(inputs)
Temporarily replace the input() function to return the given values
Use as a context manager:
- with fake_input([‘result1’, ‘result2’]):
…
Values are returned in order. If input() is called again after the last value was used, EOFError is raised.
- IPython.testing.tools.help_output_test(subcommand='')
test that
ipython [subcommand] -hworks
- IPython.testing.tools.help_all_output_test(subcommand='')
test that
ipython [subcommand] --help-allworks