pycommons.dev.tests package

Utilities for testing.

Submodules

pycommons.dev.tests.compile_and_run module

Compile and run some Python code for testing purposes.

pycommons.dev.tests.compile_and_run.compile_and_run(code, source)[source]

Compile and run some code for testing purposes.

This method first checks the types of its parameters. It then performs some superficial sanity checks on code. Then, it changes the working directory to a temporary folder which is deleted after all work is done. It then compiles and, if that was successful, executes the code fragment. Then working directory is changed back to the original directory and the temporary directory is deleted.

Parameters:
  • code (str) – the code to be compiled and run

  • source (str) – the source of the code

Raises:
  • TypeError – if code or source are not strings

  • ValueError – if any parameter has an invalid value or if the code execution fails

>>> wd = getcwd()
>>> try:
...     compile_and_run(None, "bla")
:rtype: :sphinx_autodoc_typehints_type:`\:py\:obj\:\`None\``
... except TypeError as te:
...     print(te)
descriptor '__len__' requires a 'str' object but received a 'NoneType'
>>> wd == getcwd()
True
>>> try:
...     compile_and_run(1, "bla")
... except TypeError as te:
...     print(te)
descriptor '__len__' requires a 'str' object but received a 'int'
>>> wd == getcwd()
True
>>> try:
...     compile_and_run("x=5", None)
... except TypeError as te:
...     print(te)
descriptor '__len__' requires a 'str' object but received a 'NoneType'
>>> wd == getcwd()
True
>>> try:
...     compile_and_run("x=5", 1)
... except TypeError as te:
...     print(te)
descriptor '__len__' requires a 'str' object but received a 'int'
>>> wd == getcwd()
True
>>> try:
...     compile_and_run(None, None)
... except TypeError as te:
...     print(te)
descriptor '__len__' requires a 'str' object but received a 'NoneType'
>>> wd == getcwd()
True
>>> try:
...     compile_and_run("x=5", "")
... except ValueError as ve:
...     print(ve)
Invalid source: ''.
>>> wd == getcwd()
True
>>> try:
...     compile_and_run("x=5", " ")
... except ValueError as ve:
...     print(ve)
Source cannot be only white space, but ' ' is.
>>> wd == getcwd()
True
>>> try:
...     compile_and_run("", "x")
... except ValueError as ve:
...     print(ve)
Code '' from 'x' is empty?
>>> wd == getcwd()
True
>>> try:
...     compile_and_run(" ", "x")
... except ValueError as ve:
...     print(ve)
Code ' ' from 'x' consists of only white space!
>>> wd == getcwd()
True
>>> try:
...     compile_and_run("ä ", "x")
... except ValueError as ve:
...     print(ve)
Code 'ä ' from 'x' contains non-ASCII characters.
>>> wd == getcwd()
True
>>> from contextlib import redirect_stdout
>>> from io import StringIO
>>> sio = StringIO()
>>> try:
...     with redirect_stdout(sio):
...         compile_and_run("<>-sdf/%'!234", "src")
... except ValueError as ve:
...     print(ve)
Error when compiling 'src'.
>>> wd == getcwd()
True
>>> try:
...     with redirect_stdout(sio):
...         compile_and_run("1/0", "src")
... except ValueError as ve:
...     print(ve)
Error when executing 'src'.
>>> wd == getcwd()
True
>>> sio = StringIO()
>>> with redirect_stdout(sio):
...     compile_and_run("print(1)", "src")
>>> s = sio.getvalue().splitlines()[-1]
>>> s[s.index("Success"):]
"Successfully finished executing code from 'src'."
>>> wd == getcwd()
True

pycommons.dev.tests.examples_in_dir module

Test all the example code in a directory.

pycommons.dev.tests.examples_in_dir.check_examples_in_dir(directory, recurse=True)[source]

Test all the examples in a directory.

Parameters:
  • directory (str) – the directory

  • recurse (bool, default: True) – should we recurse into sub-directories (if any)?

Return type:

int

Returns:

the total number of examples executed

Raises:
  • TypeError – if directory is not a string or recurse is not a Boolean

  • ValueError – if executing the examples fails or if no examples were found

>>> try:
...     check_examples_in_dir(None, True)
... except TypeError as te:
...     print(te)
descriptor '__len__' requires a 'str' object but received a 'NoneType'
>>> try:
...     check_examples_in_dir(1, True)
... except TypeError as te:
...     print(te)
descriptor '__len__' requires a 'str' object but received a 'int'
>>> try:
...     check_examples_in_dir("x", None)
... except TypeError as te:
...     print(te)
recurse should be an instance of bool but is None.
>>> try:
...     check_examples_in_dir("y", 1)
... except TypeError as te:
...     print(te)
recurse should be an instance of bool but is int, namely '1'.
>>> from pycommons.io.temp import temp_dir
>>> from contextlib import redirect_stdout
>>> from io import StringIO
>>> with temp_dir() as td, redirect_stdout(StringIO()) as rd:
...     try:
...         check_examples_in_dir(td, True)
...     except ValueError as ve:
...         s = str(ve)
>>> print(s[:30])
No examples found in directory
>>> with temp_dir() as td, redirect_stdout(StringIO()) as rd:
...     pystring = "print('hello world!')"
...     td.resolve_inside("1.py").write_all_str(pystring)
...     td.resolve_inside("2.py").write_all_str(pystring)
...     tdx = td.resolve_inside("second")
...     tdx.ensure_dir_exists()
...     tdx.resolve_inside("1.py").write_all_str(pystring)
...     tdx.resolve_inside("2.py").write_all_str(pystring)
...     r1 = check_examples_in_dir(td, True)
...     r2 = check_examples_in_dir(td, False)
>>> print(r1)
4
>>> print(r2)
2
>>> with temp_dir() as td, redirect_stdout(StringIO()) as rd:
...     pystring = "print('hello world!')"
...     td.resolve_inside("1.py").write_all_str(pystring)
...     pyerrstring = "1 / 0"
...     td.resolve_inside("2.py").write_all_str(pyerrstring)
...     tdx = td.resolve_inside("second")
...     res = ""
...     try:
...         check_examples_in_dir(td, True)
...     except ValueError as ve:
...         res = str(ve)
>>> print(res[:20])
Error when executing

pycommons.dev.tests.examples_in_md module

Test all the Python example code in a markdown file.

pycommons.dev.tests.examples_in_md.check_examples_in_md(file)[source]

Test all the example Python codes in a markdown file.

Parameters:

file (str) – the file

Raises:
>>> from contextlib import redirect_stdout
>>> from io import StringIO
>>> with StringIO() as sio:
...     with redirect_stdout(sio):
...         check_examples_in_md(file_path(file_path(__file__).up(
...             4).resolve_inside("README.md")))
...     res = sio.getvalue()
>>> print(res[-12:].strip())
README.md'.
:rtype: :sphinx_autodoc_typehints_type:`\:py\:obj\:\`None\``
>>> ix = res.index(" Successfully executed all")
>>> print(res[ix:ix + 40].strip())
Successfully executed all 2 examples fr
>>> try:
...     check_examples_in_md(1)
... except TypeError as te:
...     print(te)
descriptor '__len__' requires a 'str' object but received a 'int'
>>> try:
...     check_examples_in_md(None)
... except TypeError as te:
...     print(te)
descriptor '__len__' requires a 'str' object but received a 'NoneType'
>>> try:
...     check_examples_in_md("")
... except ValueError as ve:
...     print(ve)
Path must not be empty.
>>> try:
...     check_examples_in_md("/")
... except ValueError as ve:
...     print(ve)
Path '/' does not identify a file.