# Testing the code¶

All additions and modifications to ASE should be tested.

Test scripts should be put in the ase/test directory. Run all tests with:

python -c "from ase.test import test; test(2)"


It is using the function:

test.test(verbosity=1, dir=None)

Runs the test scripts in ase/test.

Important

When you fix a bug, add a test to the test suite checking that it is truly fixed. Bugs sometimes come back, do not give it a second chance!

## How to fail successfully¶

The test suite provided by test.test() automatically runs all test scripts in the ase/test directory and summarizes the results.

Note

Test scripts are run from within Python using the execfile() function. Among other things, this provides the test scripts with an specialized global namespace, which means they may fail or behave differently if you try to run them directly e.g. using python testscript.py.

If a test script causes an exception to be thrown, or otherwise terminates in an unexpected way, it will show up in this summary. This is the most effective way of raising awareness about emerging conflicts and bugs during the development cycle of the latest revision.

Remember, great tests should serve a dual purpose:

Working interface
To ensure that the class’es and method’s in ASE are functional and provide the expected interface. Empirically speaking, code which is not covered by a test script tends to stop working over time.
Replicable results
Even if a calculation makes it to the end without crashing, you can never be too sure that the numerical results are consistent. Don’t just assume they are, assert() it!
ase.test.assert(expression)

Raises an AssertionError if the expression does not evaluate to True.

Example:

from ase import molecule
atoms = molecule('C60')
atoms.center(vacuum=4.0)
result = atoms.get_positions().mean(axis=0)
expected = 0.5*atoms.get_cell().diagonal()
tolerance = 1e-4
assert (abs(result - expected) < tolerance).all()


Using functions to repeat calculations with different parameters:

def test(parameter):
# setup atoms here...
atoms.set_something(parameter)
# calculations here...
assert everything_is_going_to_be_alright

if __name__ in ['__main__', '__builtin__']:
test(0.1)
test(0.3)
test(0.7)


Important

Unlike normally, the module __name__ will be set to '__builtin__' when a test script is run by the test suite.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.3140825927257538, "perplexity": 4518.6348288460495}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2018-51/segments/1544376823442.17/warc/CC-MAIN-20181210191406-20181210212906-00589.warc.gz"}