@magine
2015-02-05T08:14:02.000000Z
字数 1506
阅读 1206
实习日记
今晚被lepture前辈带去看《博物馆奇妙夜3》了,
影院前排的观众好热情啊,全程吵闹喋喋不休也是醉了。
今天上午在Satoru前辈的指导下,修正了一个vagrant provision文件里的Bug。
# 旧版
- name: log files for tristram
file: state=file path=/var/log/tristram/nginx/{{ item }} mode=0644
with_items:
- access.log
- error.log
# 新版
- name: log files for tristram
file: state=touch path=/var/log/tristram/nginx/{{ item }} mode=0644
with_items:
- access.log
- error.log
下午在lepture前辈的指导下,参照pytest的文档和flask的测试文档写了一个后台界面的登陆测试。
# filename: test_admin.py
import pytest
from flask import url_for
from tristram.models import AdminUser
# 被fixture装饰的client函数会在本脚本中的测试函数执行前被调用,
# 并返回一个flask自带的链接测试对象test_client给测试函数
# 此处还有一个额外的处理,在访问前先新建了一个admin用户
@pytest.fixture()
def client(app):
AdminUser.create_admin_user(
'namenamename',
'password'
)
return app.test_client()
# pytest执行时会自动找到所有带‘test_’前缀的函数来执行
def test_get_login(client):
rv = client.get('/admin/login/')
assert b'</form>' in rv.data
def test_login_success(client):
rv = client.post('/admin/login/', data=dict(
username='namename',
password='password'
))
assert rv.status_code == 302
assert rv.location == url_for('admin.index')
看起来很简单吧?当然,还要有对应的配置文件的支持。
# filename: conftest.py
import pytest
from tristram.app import create_app
from tristram.models import db
# 写在此处的fixture会在所有的测试开始前依次被执行
# 其优先级高于各个测试脚本中的fixture
@pytest.fixture()
def app(request):
app = create_app({
'SERVER_NAME': 'localhost',
'SQLALCHEMY_DATABASE_URI': 'sqlite://',
'MEMCACHED': ['127.0.0.1:11211'],
})
context = app.app_context()
context.push()
db.init_app(app)
db.create_all()
@request.addfinalizer
def fin():
from tristram.libs.cache import mc
mc.clear()
db.drop_all()
context.pop()
return app