You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 6, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: docs/contributing/contributions_api.rst
+84-32Lines changed: 84 additions & 32 deletions
Original file line number
Diff line number
Diff line change
@@ -30,21 +30,37 @@ repository and contribute changes back to the project master branch using pull r
30
30
Test code
31
31
=========
32
32
33
-
Test code should be used to verify new and changed functionality. There are three test suites in DroneKit-Python:
33
+
There are three test suites in DroneKit-Python:
34
34
35
35
* **Unit tests** (:file:`tests/unit`) — verify all code paths of the API.
36
36
* **Integration tests** (:file:`tests/sitl`) — verify real-world code, examples, and documentation as they would perform in a real environment.
37
37
* **Web client tests** (:file:`tests/web`) — specifically verify the Python library's capability to talk to `DroneKit Cloud <http://cloud.dronekit.io>`_.
38
38
39
+
Test code should be used to verify new and changed functionality. New tests should:
40
+
41
+
#. Verify all code paths that code can take.
42
+
#. Be concise and straightforward.
43
+
#. Be documented.
44
+
45
+
39
46
Setting up local testing
40
47
------------------------
41
48
42
-
The links below provide information on how to set up a development environment on your development computer. Changes to DroneKit can then be tested locally.
49
+
Follow the links below to set up a development environment on your Linux or Windows computer.
43
50
44
51
* :ref:`dronekit_development_linux`
45
52
* :ref:`dronekit_development_windows`
46
53
47
-
Several of the test suites use `nose <https://nose.readthedocs.org/en/latest/>`_, a Python library for writing test scripts and a command line tool for running these. When setting up your dev environment, all test dependencies will have been installed (via :file:`requirements.txt`).
54
+
The tests require additional pip modules, including `nose <https://nose.readthedocs.org/en/latest/>`_, a
55
+
Python library and tool for writing and running test scripts. These can be installed separately using either of the commands below:
56
+
57
+
.. code:: bash
58
+
59
+
# Install just the additional requirements for tests
60
+
pip install requests nose mock
61
+
62
+
# (or) Install all requirements for dronekit, tests, and building documentation
63
+
pip install -r requirements.txt
48
64
49
65
For several tests, you may be required to set an **environment variable**. In your command line, you can set the name of a variable to equal a value using the following invocation, depending on your OS:
50
66
@@ -57,41 +73,53 @@ For several tests, you may be required to set an **environment variable**. In yo
57
73
Unit tests
58
74
----------
59
75
60
-
Unit tests use *nosetests*. On any OS, enter the following command on a terminal/prompt to run the unit tests (and display a summary of the results):
76
+
All new features should be created with accompanying unit tests.
77
+
78
+
DroneKit-Python unit tests are based on the `nose <https://nose.readthedocs.org/en/latest/>`_ test framework,
79
+
and use `mock <https://docs.python.org/dev/library/unittest.mock.html>`_ to simulate objects and APIs and
80
+
ensure correct results.
81
+
82
+
To run the tests and display a summary of the results (on any OS),
83
+
navigate to the **dronekit-python** folder and enter the following
84
+
command on a terminal/prompt:
61
85
62
86
.. code:: bash
63
87
64
-
cd dronekit-python
65
-
nosetests tests/unit
88
+
nosetests dronekit.test.unit
89
+
66
90
67
-
For unit tests, `mock <https://docs.python.org/dev/library/unittest.mock.html>`_ is used to simulate objects and APIs and ensure correct results.
68
91
69
92
70
93
Writing a new unit test
71
94
^^^^^^^^^^^^^^^^^^^^^^^
72
95
73
-
Good unit tests should:
96
+
Create any file named :file:`test_XXX.py` in the :file:`tests/unit` folder to add it as a test.
97
+
Feel free to copy from existing tests to get started. When *nosetests* is run, it will add your new test to its summary.
74
98
75
-
#. Accompany all new features that are written.
76
-
#. Verify all code paths that code can take.
77
-
#. Be concise and straightforward.
99
+
Tests names should be named based on their associated Github issue (for example,
100
+
``test_12.py`` for `issue #12 <https://github.com/dronekit/dronekit-python/issues/12>`_)
101
+
or describe the functionality covered (for example, ``test_waypoints.py``
102
+
for a unit test for the waypoints API).
103
+
104
+
Use assertions to test your code is consistent. You can use the built-in Python ``assert`` macro as well as ``assert_equals`` and ``assert_not_equals``
105
+
from the ``notestools`` module:
78
106
79
-
Create any file named :file:`test_XXX.py` in the :file:`tests/unit` folder to add it as a test. Feel free to copy from existing tests to get started. When *nosetests* is run, it will add your new test to its summary.
107
+
.. note::
80
108
81
-
Tests names should refer directly to a Github issue (for example, ``test_12.py`` would refer to `issue #12 <https://github.com/dronekit/dronekit-python/issues/12>`_ or describe fully what functionality they encompass (for example, ``test_waypoints.py`` would describe a unit test for the waypoints API).
82
-
83
-
Avoiding printing any data from your test. Instead, use assertions to test your code is consistent. You can use the built-in Python ``assert`` macro as well as ``assert_equals`` from the ``nose.tools`` module:
109
+
Avoiding printing any data from your test!
84
110
85
111
.. code:: python
86
112
87
-
from nose.tools import assert_equals
113
+
from nose.tools import assert_equals, assert_not_equals
88
114
89
115
deftest_this(the_number_two):
90
116
assert the_number_two >0, '2 should be greater than zero!'
91
-
assert_equals(the_number_two, 2, '2 should equal two!'
92
-
117
+
assert_equals(the_number_two, 2, '2 should equal two!')
118
+
assert_not_equals(the_number_two, 1, '2 should equal one!')
119
+
93
120
Please add documentation to each test function describing what behavior it verifies.
94
121
122
+
95
123
Integration tests
96
124
-----------------
97
125
@@ -100,19 +128,26 @@ Integrated tests use a custom test runner that is similar to *nosetests*. On any
100
128
.. code:: bash
101
129
102
130
cd dronekit-python
103
-
python -um tests.sitl
131
+
nosetests dronekit.test.sitl
132
+
133
+
You can choose to run a specific tests. The example below shows how to run
Integrated tests use the SITL environment to run DroneKit tests against a simulated Copter. Because these tests emulate Copter in real-time, you can set several environment variables to tweak the environment that code is run in:
106
145
107
146
#. ``TEST_SPEEDUP`` - Speedup factor to SITL. Default is ``TEST_SPEEDUP=1``. You can increase this factor to speed up how long your tests take to run.
108
147
#. ``TEST_RATE`` - Sets framerate. Default is ``TEST_RATE=200`` for copter, 50 for rover, 50 for plane.
109
148
#. ``TEST_RETRY`` - Retry failed tests. Default is ``TEST_RETRY=1``. This is useful if your testing environment generates inconsistent success rates because of timing.
110
149
111
-
You can choose to test specific files by passing them as arguments:
112
150
113
-
.. code:: bash
114
-
115
-
python -um tests.sitl test_1.py test2_.py ...
116
151
117
152
Writing a new integration test
118
153
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -123,23 +158,40 @@ Integration tests should be written or improved whenever:
123
158
#. Example code or documentation has been added.
124
159
#. A feature could not be tested by unit tests alone (e.g. timing issues, mode changing, etc.)
125
160
126
-
You can write a new integrated test by adding a filewith the naming scheme :file:`test_XXX.py` to the :file:`tests/sitl` directory. In this file, functions with the prefix ``test_`` will be called with the ``local_connect`` parameter. For example:
161
+
You can write a new integrated test by adding (or copying) a file with the naming scheme :file:`test_XXX.py` to the :file:`tests/sitl` directory.
162
+
163
+
Tests names should be named based on their associated Github issue (for example,
164
+
``test_12.py`` for `issue #12 <https://github.com/dronekit/dronekit-python/issues/12>`_)
165
+
or describe the functionality covered (for example, ``test_waypoints.py``
166
+
for an integration test for the waypoints API).
167
+
168
+
Tests should minimally use the imports shown below and decorate test functions with ``@with_sitl``
169
+
(this sets up the test and passes in a connection string for SITL).
127
170
128
171
.. code:: python
129
172
130
-
from testlib import assert_equals
173
+
from dronekit import connect
174
+
from dronekit.test import with_sitl
175
+
from nose.tools import assert_equals, assert_not_equals
176
+
177
+
@with_sitl
178
+
deftest_something(connpath):
179
+
vehicle = connect(connpath)
180
+
181
+
# Test using assert, assert_equals and assert_not_equals
Use assertions to test your code is consistent. You can use the built-in Python ``assert`` macro as well as ``assert_equals`` and ``assert_not_equals``
188
+
from the ``testlib`` module:
137
189
138
-
This checks to see that the parameter objectis of type`float`.
190
+
.. note::
139
191
140
-
Tests names should refer directly to a Github issue (for example, ``test_12.py`` would refer to `issue #12 <https://github.com/dronekit/dronekit-python/issues/12>`_ or describe fully what functionality they encompass (for example, ``test_waypoints.py`` would describe a unit test for the waypoints API).
192
+
Avoiding printing any data from your test!
193
+
141
194
142
-
Avoiding printing any data from your test. Instead, use assertions to test your code is consistent. You can use the built-in Python ``assert`` macro as well as``assert_equals``from the ``testlib`` module:
0 commit comments