Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 33584e9

Browse files
committed
Merge pull request dronekit#495 from dronekit/hgw_test_update_dkpy2
Update test instructions to dkpy2
2 parents 6c16a29 + 75e1d8b commit 33584e9

File tree

1 file changed

+84
-32
lines changed

1 file changed

+84
-32
lines changed

docs/contributing/contributions_api.rst

Lines changed: 84 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,37 @@ repository and contribute changes back to the project master branch using pull r
3030
Test code
3131
=========
3232

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:
3434

3535
* **Unit tests** (:file:`tests/unit`) — verify all code paths of the API.
3636
* **Integration tests** (:file:`tests/sitl`) — verify real-world code, examples, and documentation as they would perform in a real environment.
3737
* **Web client tests** (:file:`tests/web`) — specifically verify the Python library's capability to talk to `DroneKit Cloud <http://cloud.dronekit.io>`_.
3838

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+
3946
Setting up local testing
4047
------------------------
4148

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.
4350

4451
* :ref:`dronekit_development_linux`
4552
* :ref:`dronekit_development_windows`
4653

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
4864
4965
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:
5066

@@ -57,41 +73,53 @@ For several tests, you may be required to set an **environment variable**. In yo
5773
Unit tests
5874
----------
5975

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:
6185

6286
.. code:: bash
6387
64-
cd dronekit-python
65-
nosetests tests/unit
88+
nosetests dronekit.test.unit
89+
6690
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.
6891
6992
7093
Writing a new unit test
7194
^^^^^^^^^^^^^^^^^^^^^^^
7295

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.
7498

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:
78106

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::
80108

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!
84110

85111
.. code:: python
86112
87-
from nose.tools import assert_equals
113+
from nose.tools import assert_equals, assert_not_equals
88114
89115
def test_this(the_number_two):
90116
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+
93120
Please add documentation to each test function describing what behavior it verifies.
94121

122+
95123
Integration tests
96124
-----------------
97125

@@ -100,19 +128,26 @@ Integrated tests use a custom test runner that is similar to *nosetests*. On any
100128
.. code:: bash
101129
102130
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
134+
**\dronekit-python\dronekit\test\sitl\test_12.py**.
135+
136+
.. code:: bash
137+
138+
nosetests dronekit.test.sitl.test_12
139+
140+
141+
Configuring the test environment
142+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
104143

105144
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:
106145

107146
#. ``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.
108147
#. ``TEST_RATE`` - Sets framerate. Default is ``TEST_RATE=200`` for copter, 50 for rover, 50 for plane.
109148
#. ``TEST_RETRY`` - Retry failed tests. Default is ``TEST_RETRY=1``. This is useful if your testing environment generates inconsistent success rates because of timing.
110149

111-
You can choose to test specific files by passing them as arguments:
112150

113-
.. code:: bash
114-
115-
python -um tests.sitl test_1.py test2_.py ...
116151

117152
Writing a new integration test
118153
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -123,23 +158,40 @@ Integration tests should be written or improved whenever:
123158
#. Example code or documentation has been added.
124159
#. A feature could not be tested by unit tests alone (e.g. timing issues, mode changing, etc.)
125160

126-
You can write a new integrated test by adding a file with 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).
127170

128171
.. code:: python
129172
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+
def test_something(connpath):
179+
vehicle = connect(connpath)
180+
181+
# Test using assert, assert_equals and assert_not_equals
182+
...
183+
184+
vehicle.close()
131185
132-
def test_parameters(local_connect):
133-
v = local_connect().get_vehicles()[0]
134186
135-
# Simple parameter checks
136-
assert_equals(type(v.parameters['THR_MIN']), float)
187+
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:
137189

138-
This checks to see that the parameter object is of type `float`.
190+
.. note::
139191

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+
141194

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:
143195

144196
.. code:: python
145197

0 commit comments

Comments
 (0)