GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing docs.stackstorm.com — Pack Testing — StackStorm 3.9.0 documentation Pack TestingThis section includes information on pack testing - where to put the tests, how to write the tests, mock classes which can be used to make testing easier, etc. Test File Locations and NamesAll the test files should go into Test files should follow the following naming conventions:
General Testing ConventionsMost of the StackStorm packs interact with a third party API or tool. Writing full-blown integration and end-to-end tests would be very time consuming and hard, so the convention is to write unit tests and mock the responses and method calls where necessary. Base Test Classes and Mock ClassesTo make testing easier, StackStorm provides some base test and mock classes you can use in the tests. Base Test Classes
Mock Classes
DependenciesIn addition to the StackStorm and pack dependencies listed in
The sensors ( For example, if you have an action file named import parse_xml Keep in mind that both sensor and action modules are not namespaced which means sensor and action module names need to be unique to avoid conflicts. FixturesAll the fixture data such as raw HTTP responses and similar, should be stored in files in the
To retrieve raw content of the fixture file you can use the Instantiating and obtaining class instancesWhen obtaining a sensor or an action class instance you should use This is important because those two methods mimic the class initialization process which is otherwise performed inside the action/sensor wrapper. Sensor tests: class MySensorSensorTestCase(BaseSensorTestCase): sensor_cls = MySensor def test_method(self): sensor = self.get_sensor_instance(config={'foo': 'bar'}) sensor.poll() # ... Action tests: class MyActionActionTestCase(BaseActionTestCase): action_cls = MyAction def test_method(self): action = self.get_action_instance(config={'foo': 'bar'}) result = action.run() # ... Action alias tests: class MyActionAliasTestCase(BaseActionTestCase): action_alias_name = 'my_alias' def test_method(self): action_alias_db = self.action_alias_db As you can see, when testing aliases you need to specify the name of the alias which is to be
tested. This alias is automatically retrieved from disk and available via Sample TestsHere’s some example tests:
Running TestsNote For this script to work correctly, all the StackStorm components need to be
in If that is not the case, you need to set git clone https://github.com/StackStorm/st2.git /tmp/st2 ST2_REPO_PATH=/tmp/st2 st2-run-pack-tests -p <pack path> To run all the tests in a particular pack you can use the st2-run-pack-tests -p <pack path> [-f test module name with optional test class and method name] For example: st2-run-pack-tests -p /data/packs/docker/ By default, this script will create and use a new temporary virtual environment for each pack test run and install all the dependencies which are required to run the tests inside this virtual environment. If you want to avoid virtual environment creation (e.g. the virtual environment already exists or
you have created one manually), you can pass the If you are running this script inside a development VM (st2vagrant), you can safely pass the In addition to that, if all the pack dependencies are already installed and you want to skip
installing and updating the dependencies, you can pass the For example: st2-run-pack-tests -p /data/packs/docker/ -x -j Alternatively, if a virtual environment for tests has already been created during previous tool
invocation, you can skip updating of the virtual environment and just run the tests by using the
# First run - create tests virtual environment and run the tests st2-run-pack-tests -p /data/packs/docker/ # Second (and subsequent) runs - just run the tests and re-use the existing # virtual environment which has been created during the previous script # invocation. st2-run-pack-tests -p /data/packs/docker/ -j If you only want to run a specific test file or a method in a test method, you can do that using
# NOTE: The following examples assume test_sensor_docker_sensor.py file exists in the # /data/packs/docker/tests/ directory and that the file contains DockerSensorTestCase # class name with the "test_poll" method. # Run all the tests inside that test file / module st2-run-pack-tests -p /data/packs/docker/ -f test_sensor_docker_sensor.py # Run all tests in a specific test class st2-run-pack-tests -p /data/packs/docker/ -f test_sensor_docker_sensor.py::DockerSensorTestCase # Run a single test method from a specific test file st2-run-pack-tests -p /data/packs/docker/ -f test_sensor_docker_sensor.py::DockerSensorTestCase::test_poll As more tests are developed it is always a good idea to determine how much code has been covered by
the tests and how much remains un-tested. Calculated test coverage can be printed out using the
st2-run-pack-tests -c -p /data/packs/docker/ The command will print out test coverage to Understanding how long a test takes to run is sometimes important. Timing metrics can be enabled
via the st2-run-pack-tests -t -p /data/packs/docker/ Lint Tools and ScriptsIn addition to tests, the st2sdk repository and package also ships with various other tools and lint scripts which allow you to catch common errors and typos automatically and early. For more information on those scripts and how to use them, please refer to the README in the st2sdk repository. Continuous IntegrationBy default, the lint scripts mentioned above and tests for all the packs run
on every commit to |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |