File: tests.rst

package info (click to toggle)
python-ase 3.24.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 15,448 kB
  • sloc: python: 144,945; xml: 2,728; makefile: 113; javascript: 47
file content (102 lines) | stat: -rw-r--r-- 3,041 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
.. module:: ase.test

================
Testing the code
================

All additions and modifications to ASE should be tested.

.. index:: testase

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

  ase test

This requires installing ``pytest``, ``pytest-mock`` and ``pytest-xdist``.
You can install these dependencies automatically by running::

  pip install ase[test]

Tests for some calculators require data files in
https://gitlab.com/ase/ase-datafiles.
You can enable these tests by running::

  pip install --user --upgrade git+https://gitlab.com/ase/ase-datafiles.git

See ``ase test --help`` for more information.

You can also run ``pytest`` directly from within the ``ase.test`` directory.

.. 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 add a test
=================

Create a module somewhere under ``ase.test``.  Make sure its name
starts with ``test_``.  Inside the module, each test should be a
function whose name starts with ``test_``.  This ensures that pytest
finds the test.  Use ``ase test --list`` to see which tests it will
find.

You may note that many tests do not follow these rules.
These are older tests.  We expect to port them one day.

How to fail successfully
========================

The test suite provided by :mod:`ase.test` automatically runs all test
scripts in the :git:`ase/test` directory and summarizes the results.

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 :term:`class`'es and :term:`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, :func:`assert` it!

.. function:: assert(expression)

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



Example::

  from ase import molecule

  def test_c60():
      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()


To run the same test with different inputs, use pytest fixtures.
For example::

  @pytest.mark.parametrize('parameter', [0.1, 0.3, 0.7])
  def test_something(parameter):
      # setup atoms here...
      atoms.set_something(parameter)
      # calculations here...
      assert everything_is_going_to_be_alright