scripttest – test command-line scripts¶
Helpers for testing command-line scripts
Module Contents¶
- class scripttest.TestFileEnvironment(base_path=None, template_path=None, environ=None, cwd=None, start_clear=True, ignore_paths=None, ignore_hidden=True, ignore_temp_paths=None, capture_temp=False, assert_no_temp=False, split_cmd=True)[source]¶
This represents an environment in which files will be written, and scripts will be run.
- __init__(base_path=None, template_path=None, environ=None, cwd=None, start_clear=True, ignore_paths=None, ignore_hidden=True, ignore_temp_paths=None, capture_temp=False, assert_no_temp=False, split_cmd=True)[source]¶
Creates an environment.
base_pathis used as the current working directory, and generally where changes are looked for. If not given, it will be the directory of the calling script plustest-output/.template_pathis the directory to look for template files, which are files you’ll explicitly add to the environment. This is done with.writefile().environis the operating system environment,os.environif not given.cwdis the working directory,base_pathby default.If
start_clearis true (default) then thebase_pathwill be cleared (all files deleted) when an instance is created. You can also use.clear()to clear the files.ignore_pathsis a set of specific filenames that should be ignored when created in the environment.ignore_hiddenmeans, if true (default) that filenames and directories starting with'.'will be ignored.capture_tempwill put temporary files inside the environment (using$TMPDIR). You can then assert that no temporary files are left using.assert_no_temp().
- assert_no_temp()[source]¶
If you use
capture_tempthen you can use this to make sure no files have been left in the temporary directory
- run(script, *args, **kw)[source]¶
Run the command, with the given arguments. The
scriptargument can have space-separated arguments, or you can use the positional arguments.Keywords allowed are:
expect_error: (default False)Don’t raise an exception in case of errors
expect_stderr: (defaultexpect_error)Don’t raise an exception if anything is printed to stderr
stdin: (default"")Input to the script
cwd: (defaultself.cwd)The working directory to run in (default
base_path)quiet: (default False)When there’s an error (return code != 0), do not print stdout/stderr
Returns a ProcResult object.
Objects that are returned¶
These objects are returned when you use env.run(...). The
ProcResult object is returned, and it has .files_updated,
.files_created, and .files_deleted which are dictionaries of
FoundFile and FoundDir. The files in .files_deleted represent
the pre-deletion state of the file; the other files represent the
state of the files after the command is run.
and
.files_deleted. These objects dictionary
- class scripttest.ProcResult(test_env, args, stdin, stdout, stderr, returncode, files_before, files_after)[source]¶
Represents the results of running a command in TestFileEnvironment.
Attributes to pay particular attention to:
- class scripttest.FoundFile(base_path, path)[source]¶
Represents a single file found as the result of a command.
Has attributes:
path:The path of the file, relative to the
base_pathfull:The full path
bytes:The contents of the file.
stat:The results of
os.stat. Alsomtimeandsizecontain the.st_mtimeand.st_sizeof the stat.mtime:The modification time of the file.
size:The size (in bytes) of the file.
You may use the
inoperator with these objects (tested against the contents of the file), and the.mustcontain()method.