Skip to content

Commit d866d6c

Browse files
committed
test:优化测试用例
1 parent aefc401 commit d866d6c

7 files changed

Lines changed: 73 additions & 45 deletions

File tree

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Lin-CMS==0.3.0a8
22
Flask-Cors==3.0.9
33
python-dotenv==0.15.0
4-
pytest==6.1.2
4+
pytest-ordering==0.6
55
isort==5.6.4
66
black==20.8b1

tests/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import json
22
import os
33

4+
import pytest
5+
46
from app.app import create_app
57
from app.model.lin import (
68
Group,
@@ -11,6 +13,8 @@
1113
UserIdentity,
1214
)
1315

16+
from .config import password, username
17+
1418
app = create_app(
1519
group_model=Group,
1620
user_model=User,
@@ -21,6 +25,20 @@
2125
)
2226

2327

28+
@pytest.fixture()
29+
def fixtureFunc():
30+
with app.test_client() as c:
31+
rv = c.post(
32+
"/cms/user/login",
33+
headers={"Content-Type": "application/json"},
34+
json={"username": username, "password": password},
35+
)
36+
json_data = rv.get_json()
37+
assert json_data.get("access_token") != None
38+
assert rv.status_code == 200
39+
write_token(json_data)
40+
41+
2442
def get_file_path():
2543
pytest_cache_dir_path = os.getcwd() + os.path.sep + ".pytest_cache"
2644
if not os.path.exists(pytest_cache_dir_path):

tests/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
username = "root"
2+
password = "123456"

tests/test_a_user.py

Lines changed: 0 additions & 30 deletions
This file was deleted.
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,19 @@
22
:copyright: © 2020 by the Lin team.
33
:license: MIT, see LICENSE for more details.
44
"""
5-
from . import app, get_token
5+
from . import app, fixtureFunc, get_token
66

77

8-
def test_a_authority():
8+
def test_permission(fixtureFunc):
99
with app.test_client() as c:
1010
rv = c.get(
11-
"/cms/admin/permission", headers={"Authorization": "Bearer " + get_token()}
11+
"/cms/admin/permission",
12+
headers={"Authorization": "Bearer " + get_token()},
1213
)
1314
assert rv.status_code == 200
1415

1516

16-
def test_b_get_admin_users():
17+
def test_get_root_users(fixtureFunc):
1718
with app.test_client() as c:
1819
rv = c.get(
1920
"/cms/admin/users", headers={"Authorization": "Bearer " + get_token()}
Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,64 @@
22
:copyright: © 2020 by the Lin team.
33
:license: MIT, see LICENSE for more details.
44
"""
5-
from . import app, get_token
5+
import pytest
66

7+
from . import app, fixtureFunc, get_token
78

8-
def test_a_create():
9+
10+
@pytest.mark.run(order=1)
11+
def test_create(fixtureFunc):
912
with app.test_client() as c:
1013
rv = c.post(
1114
"/v1/book",
15+
headers={"Authorization": "Bearer " + get_token()},
1216
json={
1317
"title": "create",
1418
"author": "pedro",
15-
"summary": "在写这章之前,笔者一直很踌躇,因为我并没有多年的开发经验,甚至是一年都没有。换言之,我还没有一个良好的软件开发习惯,没有一个标准的开发约束,如果你和我一样,那么请你一定要仔细阅读本小节,并且开始尝试认真,仔细的做单测,它将会让你受益匪浅。",
19+
"summary": "summary",
1620
"image": "https://img3.doubanio.com/lpic/s1470003.jpg",
1721
},
1822
)
1923
assert rv.status_code == 201 or rv.get_json().get("code") == 10030
2024

2125

22-
def test_b_get_books():
26+
@pytest.mark.run(order=2)
27+
def test_get_books():
2328
with app.test_client() as c:
24-
rv = c.get("/v1/book", headers={"Authorization": "Bearer " + get_token()})
29+
rv = c.get("/v1/book", headers={"Authorization": "Bearer "})
2530
assert rv.status_code == 200
2631

2732

28-
def test_c_update():
33+
@pytest.mark.run(order=3)
34+
def test_update(fixtureFunc):
2935
with app.test_client() as c:
36+
id = (
37+
c.get("/v1/book", headers={"Authorization": "Bearer "})
38+
.get_json()[-1]
39+
.get("id")
40+
)
3041
rv = c.put(
31-
"/v1/book/1",
42+
"/v1/book/{}".format(id),
43+
headers={"Authorization": "Bearer " + get_token()},
3244
json={
3345
"title": "update",
3446
"author": "pedro & erik",
35-
"summary": "在写这章之前,笔者一直很踌躇,因为我并没有多年的开发经验,甚至是一年都没有。换言之,我还没有一个良好的软件开发习惯,没有一个标准的开发约束,如果你和我一样",
47+
"summary": "summary",
3648
"image": "https://img3.doubanio.com/lpic/s1470003.jpg",
3749
},
3850
)
3951
assert rv.status_code == 201
4052

4153

42-
def test_d_delete():
54+
@pytest.mark.run(order=4)
55+
def test_delete():
4356
with app.test_client() as c:
44-
rv = c.delete("/v1/book/1", headers={"Authorization": "Bearer " + get_token()})
57+
id = (
58+
c.get("/v1/book", headers={"Authorization": "Bearer "})
59+
.get_json()[-1]
60+
.get("id")
61+
)
62+
rv = c.delete(
63+
"/v1/book/{}".format(id), headers={"Authorization": "Bearer " + get_token()}
64+
)
4565
assert rv.status_code == 201

tests/test_user.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""
2+
:copyright: © 2020 by the Lin team.
3+
:license: MIT, see LICENSE for more details.
4+
"""
5+
6+
7+
from . import app, fixtureFunc, get_token
8+
9+
10+
def test_change_nickname(fixtureFunc):
11+
with app.test_client() as c:
12+
rv = c.put(
13+
"/cms/user",
14+
headers={"Authorization": "Bearer " + get_token()},
15+
json={"nickname": "tester"},
16+
)
17+
assert rv.status_code == 201

0 commit comments

Comments
 (0)