Page author: @cmoineau
Python tests
Writing tests
To test the library, we are using the framework unittest.
If you develop a new feature to the python API you have to write unit test.
If you want to create your own test, you can create a script in the folder python/tests
.
Your script name have to be named test_*.py
in order to be automatically run by the unittest
framework.
Then follow this template :
import unittest
class test_name(unittest.TestCase):
"""
The class needs to inherit unittest.TestCase, the name doesn't matter and the class doesn't need to be instantiated.
"""
def setUp(self):
"""
Method called before each test
"""
pass
def tearDown(self):
"""
Method called after a test, even if it failed.
Can be used to clean variables
"""
pass
def test_X(self):
"""
Method called to test a functionality. It needs to be named test_* to be called.
"""
"""
To test the functions you can use one of the following method :
- self.assertEqual(a, b)
- self.assertNotEqual(a, b)
- self.assertTrue(a)
- self.assertFalse(a)
- self.assertIs(a, b)
- self.assertIsNot(a, b)
- self.assertIsNotNone(a)
- self.assertIn(a, b)
- self.assertNotIn(a, b)
- self.assertIsInstance(a, b)
"""
"""
You can use the following decorator :
- @unittest.skip(display_text)
- @unittest.skipIf(cond, display_text)
- @unittest.skipUnless(cond, display_text)
- @unittest.expectedFailure()
"""
"""
You can test that a function raises error by putting it in a block :
with self.assertRaises(TypeError):
You can replace TypeError by the type of message you are expecting
"""
pass
if __name__ == '__main__':
"""
You need to add this line for the tests to be run.
"""
unittest.main()
Running tests
If you want to run the unit test use the command :
python -m unittest discover -s tests -v 1> /dev/null
If you wan to only run your test, you can just type the command
python SCRIPT_PATH
🌻
Tip: run you tests with pytest for a cleaner ouput Pytests understands unittests and renders a more concise output
pip install pytest
python -m pytest <your_test>.py