Skip to content

Commit 5c7ecbe

Browse files
committed
add pytest
1 parent f6fb166 commit 5c7ecbe

File tree

5 files changed

+211
-164
lines changed

5 files changed

+211
-164
lines changed

Diff for: README.md

+11-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ To run the Flask application, follow these steps:
2222
>**Note** - In Windows, the `venv` does not have a `bin` directory. Therefore, you'd use the analogous command shown below:
2323
2424
```bash
25-
source venv\Scripts\activate
25+
source .venv\Scripts\activate
2626
```
2727
2828
1. **Install the app as an editable package:**
@@ -44,18 +44,26 @@ To run the Flask application, follow these steps:
4444
python3 -m flask --app src.flaskapp run --reload
4545
```
4646
47-
### Run the tests
47+
### Development
4848
4949
1. **Inside your virtual environment, execute the following command to install the development requirements:**
5050
5151
```bash
5252
pip install -r requirements-dev.txt
5353
```
5454
55+
1. **Execute the following command to install the pre commit hooks:**
56+
57+
```bash
58+
pre-commit install
59+
```
60+
61+
### Testing
62+
5563
1. **Execute the following command to run the tests**
5664
5765
```bash
58-
python flask_test.py
66+
pytest
5967
```
6068
6169
## API Documentation
@@ -218,5 +226,3 @@ The API will return these error types when the request fails:
218226
"total_executions": 10
219227
}
220228
```
221-
222-

Diff for: tests/conftest.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import os
2+
3+
import pytest
4+
5+
from flaskapp import create_app
6+
from flaskapp.database.models import db
7+
8+
9+
@pytest.fixture(scope="session")
10+
def app_with_db():
11+
"""Session-wide test `Flask` application."""
12+
config_override = {
13+
"TESTING": True,
14+
# Allows for override of database to separate test from dev environments
15+
"SQLALCHEMY_DATABASE_URI": os.environ.get("TEST_DATABASE_URL", os.environ.get("DATABASE_FILENAME")),
16+
}
17+
app = create_app(config_override)
18+
19+
with app.app_context():
20+
engines = db.engines
21+
22+
engine_cleanup = []
23+
24+
for key, engine in engines.items():
25+
connection = engine.connect()
26+
transaction = connection.begin()
27+
engines[key] = connection
28+
engine_cleanup.append((key, engine, connection, transaction))
29+
30+
yield app
31+
32+
for key, engine, connection, transaction in engine_cleanup:
33+
try:
34+
transaction.rollback()
35+
connection.close()
36+
except Exception:
37+
connection.close()
38+
engines[key] = engine

Diff for: tests/flask_test.py

-157
This file was deleted.

Diff for: tests/test_app.py

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def client(app_with_db):
6+
return app_with_db.test_client()
7+
8+
9+
def test_index(client):
10+
"""Test index page"""
11+
response = client.get("/")
12+
body = response.get_json()
13+
14+
assert response.status_code == 200
15+
assert body["success"] is True
16+
assert body["message"] == "Welcome to the test case management API"
17+
18+
19+
def test_retrieve_tests(client):
20+
"""Test retrieve tests"""
21+
response = client.get("/tests")
22+
body = response.get_json()
23+
24+
assert response.status_code == 200
25+
assert body["success"] is True
26+
assert body["test_cases"]
27+
28+
29+
def test_405_using_wrong_method_to_retrieve_tests(client):
30+
"""Test 405 using wrong method to retrieve tests"""
31+
response = client.patch("/tests")
32+
body = response.get_json()
33+
34+
assert response.status_code == 405
35+
assert body["success"] is False
36+
assert body["message"]
37+
38+
39+
def test_create_new_test(client):
40+
"""test create new test"""
41+
response = client.post(
42+
"/tests",
43+
json={
44+
"name": "New Test Case",
45+
"description": "New Test Case Description",
46+
},
47+
)
48+
body = response.get_json()
49+
50+
assert response.status_code == 200
51+
assert body["success"] is True
52+
53+
54+
def test_400_create_new_test_without_name(client):
55+
"""test create new test without providing name"""
56+
response = client.post("/tests", json={"testing": "xxx"})
57+
body = response.get_json()
58+
59+
assert response.status_code == 400
60+
assert body["success"] is False
61+
assert body["message"]
62+
63+
64+
def test_405_creation_not_allowed(client):
65+
"""test 405 creation not allowed"""
66+
response = client.post(
67+
"/tests/45",
68+
json={
69+
"name": "New Test Case",
70+
"description": "New Test Case Description",
71+
},
72+
)
73+
body = response.get_json()
74+
75+
assert response.status_code == 405
76+
assert body["success"] is False
77+
assert body["message"]
78+
79+
80+
def test_get_specific_test(client):
81+
"""Test get specific test with id"""
82+
response = client.get("/tests/1")
83+
body = response.get_json()
84+
85+
assert response.status_code == 200
86+
assert body["success"] is True
87+
assert len(body["test_case"])
88+
89+
90+
def test_get_nonexistent_test(client):
91+
"""Test get non existent test"""
92+
response = client.get("/tests/10000")
93+
body = response.get_json()
94+
95+
assert response.status_code == 404
96+
assert body["success"] is False
97+
assert body["message"]
98+
99+
100+
def test_update_test(client):
101+
"""Test update test"""
102+
response = client.patch("/tests/1", json={"name": "Updated Test Case"})
103+
body = response.get_json()
104+
105+
assert response.status_code == 200
106+
assert body["success"] is True
107+
assert body["test_case"]
108+
109+
110+
def test_update_test_without_name(client):
111+
"""Test update test without providing name"""
112+
response = client.patch("/tests/1", json={"testing": "Updated Test Case"})
113+
body = response.get_json()
114+
115+
assert response.status_code == 400
116+
assert body["success"] is False
117+
assert body["message"]
118+
119+
120+
def test_404_delete_nonexistent_test(client):
121+
"""test 404 delete nonexistent test"""
122+
response = client.delete("/tests/10000")
123+
body = response.get_json()
124+
125+
assert response.status_code == 404
126+
assert body["success"] is False
127+
assert body["message"]
128+
129+
130+
def test_get_execution_results(client):
131+
"""Test get execution results"""
132+
response = client.get("/executions/1")
133+
body = response.get_json()
134+
135+
assert response.status_code == 200
136+
assert body["success"] is True
137+
assert body["executions"]
138+
assert body["asset"]
139+
assert body["total_executions"]
140+
141+
142+
def test_add_execution_results(client):
143+
"""Test add execution result"""
144+
response = client.post(
145+
"/executions",
146+
json={
147+
"asset_id": "1",
148+
"test_case_id": "1",
149+
"status": True,
150+
"details": "Success",
151+
},
152+
)
153+
body = response.get_json()
154+
155+
assert response.status_code == 200
156+
assert body["success"] is True
157+
assert body["execution"]
158+
assert body["total_executions"]

0 commit comments

Comments
 (0)