-
-
Notifications
You must be signed in to change notification settings - Fork 801
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added first example to plugin unit test (#8636)
* Start documentation chapter for plugin tests * Added env variables to the doc * Fix style errors * Further style bugs * Reformat environment variables * Reformat environment variables * Add comments from wolflu05 * Add text to the intro * Added first example the plugin unit test * Addred line it function * Typo * Typo * Typo
- Loading branch information
1 parent
64dc565
commit 5aae93e
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
--- | ||
Title: Unit Tests | ||
--- | ||
|
||
## Unit Tests | ||
For complicated plugins it makes sense to add unit tests the code to ensure | ||
that plugins work correctly and are compatible with future versions too. | ||
You can run these tests as part of your ci against the current stable and | ||
latest tag to get notified when something breaks before it gets released as | ||
part of stable. InvenTree offers a framework for testing. Please refer | ||
to [Unit Tests](../../develop/contributing.md) for more information. | ||
|
||
### Prerequisites | ||
For plugin testing the following environment variables must be set to True: | ||
|
||
| Name | Function | Value | | ||
| --- | --- | --- | | ||
| INVENTREE_PLUGINS_ENABLED | Enables the use of 3rd party plugins | True | | ||
| INVENTREE_PLUGIN_TESTING | Enables enables all plugins no matter of their active state in the db or built-in flag | True | | ||
| INVENTREE_PLUGIN_TESTING_SETUP | Enables the url mixin | True | | ||
|
||
### Test program | ||
|
||
A file called test_plugin_name.py should be added to the plugin directory. It can have the | ||
following structure: | ||
|
||
``` | ||
# Basic unit tests for the plugin | ||
from InvenTree.unit_test import InvenTreeTestCase | ||
class TestMyPlugin(InvenTreeTestCase): | ||
def test_my_function(self): | ||
do some work here... | ||
``` | ||
|
||
The test can be executed using invoke: | ||
|
||
``` | ||
invoke dev.test -r module.file.class | ||
``` | ||
|
||
Plugins are usually installed outside of the InventTree directory, e.g. in .local/lib/... | ||
I that case module must be omitted. | ||
|
||
``` | ||
invoke dev.test -r plugin_directory.test_plugin_name.TestMyPlugin | ||
``` | ||
|
||
### do some work here... A simple Example | ||
A simple example is shown here. Assume the plugin has a function that converts a price string | ||
that comes from a supplier API to a float value. The price might have the form "1.456,34 €". | ||
It can be different based on country and local settings. | ||
The function in the plugin will convert it to a float 1456.34. It is in the class MySupplier | ||
and has the following structure: | ||
|
||
``` | ||
class MySupplier(): | ||
def reformat_price(self, string_price): | ||
... | ||
return float_price | ||
``` | ||
|
||
This function needs to be tested. The test can look like this: | ||
|
||
``` | ||
from .myplugin import MySupplier | ||
def test_reformat_price(self): | ||
self.assertEqual(MySupplier.reformat_price(self, '1.456,34 €'), 1456.34) | ||
self.assertEqual(MySupplier.reformat_price(self, '1,45645 €'), 1.45645) | ||
self.assertEqual(MySupplier.reformat_price(self, '1,56 $'), 1.56) | ||
self.assertEqual(MySupplier.reformat_price(self, ''), 0) | ||
self.assertEqual(MySupplier.reformat_price(self, 'Mumpitz'), 0) | ||
``` | ||
|
||
The function assertEqual flags an error in case the two arguments are not equal. In equal case | ||
no error is flagged and the test passes. The test function tests five different | ||
input variations. More might be added based on the requirements. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.