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
title: Sivayanama - Testing OpsRamp APIs with PyTest Fixtures
2
+
title: Testing OpsRamp APIs with PyTest Fixtures
3
3
date: 2023-07-26T06:58:31.750Z
4
4
featuredBlog: true
5
5
author: BalaSubramanian Vetrivel
@@ -8,67 +8,234 @@ disable: false
8
8
---
9
9
<!--StartFragment-->
10
10
11
-
# Sivayanama - Testing OpsRamp APIs with PyTest Fixtures
12
-
13
-
<!--EndFragment-->
11
+
# Testing OpsRamp APIs with PyTest Fixtures
14
12
15
13
16
14
17
15
OpsRamp platform provides rich set of APIs. By using these APIs customers can build soultion to automate various ITOM workflows such as discovery and monitoring , event and incident mangement and remidiation and automation. Testing these APIs, automated solution are very critical to ensure reliability and stability of the same. PyTest is a powerful python testing framework and it is widely used to test APIs. Fixtures is one of the most important capabilities of PyTest framework. In this article, we will discuss some advanced techniques of testing OpsRamp APIs using PyTest fixtures.
18
16
19
17
20
18
21
-
What Fixtures are
19
+
## What Fixtures are
22
20
23
21
PyTest fixtures are a special type of python function that provision fixed base line for testing.With the help of this base line we can ensure tests are run in reliable manner and produce consistent results and the same tests can be repeatable.
24
22
25
-
26
-
27
-
Install PyTest
23
+
## Install PyTest
28
24
29
25
Run the below command
30
26
27
+
```shell
31
28
pip install -U pytest
29
+
```
32
30
33
31
34
32
35
-
Verify PyTest installation
33
+
## Verify PyTest installation
36
34
37
35
Run the below command
38
36
39
-
pytest --version
40
-
37
+
```shell
38
+
pytest --version
41
39
pytest 7.4.0
42
40
41
+
```
43
42
44
-
45
-
Define Fixture
43
+
## Define Fixture
46
44
47
45
We can define fixtures just by decorating a simple python function with [@pytest.fixture](https://docs.pytest.org/en/6.2.x/reference.html#pytest.fixture) for example
48
46
47
+
```python
48
+
import pytest
49
+
50
+
@pytest.fixture
51
+
defhello_world():
52
+
return'Hello, Happy testing'
53
+
```
54
+
55
+
56
+
57
+
## Invoke fixtures
58
+
59
+
Test functions can invoke fixtures just by declaring the required fixtures as arguments.
49
60
61
+
```python
62
+
deftest_hello_world(hello_world):
63
+
assert hello_world =='Hello, Happy testing'
64
+
```
65
+
66
+
## Invoke OpsRamp API
67
+
68
+
Let us try to execute OpsRamp [Get Access Token API](https://develop.opsramp.com/v2/api/auth/tenancy-auth-oauth-token) as shown below.
Sometimes some of your fixtures are required for all other tests. The above Get acces token is perfect use case for this. Every API call is depending on this API. In this case you can make this fixture as “autouse fixture” by passing in autouse=True to the fixture’s decorator. The autouse fixure capability will reduce lot of reduanct requests.
64
112
65
-
Test functions can invoke fixtures just by declaring the required fixtures as arguments.
Not necessarily you have to define all fixtures on your own code. Fixtures can be provided by third party plugins as well and you are free to use them. You just need to install the required plugins. We will see an example with pytest-datadir plugin
get_bearer_token fixture is using [shared_datadir](https://pypi.org/project/pytest-datadir/) fixture from third party plugin. Shared_datadir fixture provision data folder from the current folder as pathlib.Path object.
147
+
148
+
149
+
150
+
## Fixture finalization
151
+
152
+
Usually we do teardown or cleanup acititivties for each test for many good reasons such as the following
153
+
154
+
The executed tests will not impact other tests
155
+
156
+
Do not want to have the testing environment piled up with tons of test data
157
+
158
+
Every tests should execute in a clean state
159
+
160
+
161
+
162
+
Fixtures offers very powerful teardown, cleanup capability known as finalization.
You should add the finalizer function to the request’s context object of the test as shown below
223
+
224
+
request.addfinalizer(terminate_client)
225
+
226
+
227
+
228
+
In the above example we do all the test set up on the client and execute the tests, Once the test is complete finalizer function runs and cleans up the whole thing.
229
+
230
+
231
+
232
+
## Conclusion
233
+
234
+
In this article we have seen what are PyTest Fixtures, how to define, invoke and Fixtures. Also we have seen how to leverage testing of OpsRamp APIs using these fixtures, how to do teardown activities using finalizer functions.
0 commit comments