Django项目的烟雾测试。
django-skd-smoke的Python项目详细描述
此包旨在简化烟雾测试的创建。
Usage
安装后,您应该创建新的TestCase派生自 skd_smoke.SmokeTestCase并定义烟雾测试配置。 请查看演示不同用例的Examples部分。 它们与包含公共目录的example_project目录相关 django项目
Configuration
^ TestCase的{tt4}$应该包含 具有下一个结构的每个请求的元组:
(url,status,method,{'comment':None,'initialize':None,'url_kwargs':None,'request_data':None,'user_credentials':None,'redirect_to':None})
Parameter | Description | Required |
---|---|---|
url | plain url or urlname as string | Yes |
status | expected http status code (200, 404, etc.) as ^{tt6}$ | Yes |
method | http request method (GET, POST, etc.) as string | Yes |
comment | string which is added to ^{tt7}$ of generated test method | No |
initialize | callable object to do any required initialization | No |
url_args | list or callable object which returns args list to resolve url using ^{tt8}$ | No |
url_kwargs | dict or callable object which returns kwargs dict to resolve url using ^{tt8}$ | No |
request_data | dict or callable object which returns dict to pass it into http method request | No |
user_credentials | dict or callable object which returns dict to login user using ^{tt10}$ | No |
redirect_to | plain url as string which is checked if only status is one of the next: 301, 302, 303, 307 | No |
注意!所有可调用项都将TestCase作为第一个参数,因此 你可以用它在它们之间传递状态。但是考虑到 下一步是回调使用顺序:
- initialize
- url_kwargs
- user_credentials
- request_data
Examples
所有示例都取自example_project包,可以在 存储库克隆。
- 1.简单请求演示:
- 得到200
- 获取200,请求数据为dict
- 200后
- 使用可调用的请求数据发送302
- 获取302(未经授权的访问)
- 获取200(授权访问)
- 405后(不允许使用方法)
fromdjango.contrib.authimportget_user_modelfromskd_smokeimportSmokeTestCasedefget_article_data(testcase):return{'headline':'new article'}defget_user_credentials(testcase):username='test_user'password='1234'credentials={'username':username,'password':password}User=get_user_model()new_user=User.objects.create(username=username)new_user.set_password(password)new_user.save()testcase.user=new_userreturncredentialsclassSimpleSmokeTestCase(SmokeTestCase):TESTS_CONFIGURATION=(('home',200,'GET',),# 1('home',200,'GET',{'request_data':{'scrollTop':1}}),# 2('articles:create',200,'POST',),# 3('articles:create',302,'POST',{'request_data':get_article_data}),# 4('is_authenticated',302,'GET',),# 5('is_authenticated',200,'GET',{'user_credentials':get_user_credentials}),# 6('/only_post_request/',405,'GET',),# 7)
2.使用initialize回调创建多个对象以测试对象 列表。
假设您想对文章列表页进行冒烟测试,但最初 测试数据库不包含任何您可以在这里使用initialize回调 创建多篇文章。
fromskd_smokeimportSmokeTestCasefromarticles.modelsimportArticledefcreate_articles(testcase):foriinrange(3):Article.objects.create(headline='article #%s'%i)classArticlesListSmokeTestCase(SmokeTestCase):TESTS_CONFIGURATION=(('articles:articles',200,'GET',{'initialize':create_articles}# pass your func here),)
三。使用redirect_to设置测试所需登录的匿名访问 页。
fromdjango.core.urlresolversimportreversefromskd_smokeimportSmokeTestCaseclassRedirectToSmokeTestCase(SmokeTestCase):TESTS_CONFIGURATION=(('is_authenticated',302,'GET',{'redirect_to':'%s?next=%s'%(reverse('login'),reverse('is_authenticated')),'comment':'Anonymous profile access with check of redirect url'}),)
四使用url_kwargs和user_credentials回调进行测试 所有者对新创建对象的授权访问
假设您有一篇模型文章,未发布的版本可以通过 只有它的主人。可以通过在中创建用户来测试这种情况 url_kwargs回调并将用户传输到user_credentials回调。 很遗憾,您无法从用户模型中获取密码,因为它包含 哈希密码。所以您应该以纯文本形式返回密码。
当显示404页时,让smoke测试另外两种情况我们终于有了 三个测试用例:
- 匿名访问应该显示404页。
- 一些普通的用户访问也应该显示404页。
- 只有所有者访问返回状态为200的实际项目。
fromdjango.contrib.authimportget_user_modelfromskd_smokeimportSmokeTestCasefromarticles.modelsimportArticledefcreate_user():UserModel=get_user_model()new_user=UserModel.objects.create(username='test_user')new_user.set_password('1234')new_user.save()returnnew_userdefcreate_unpublished_article(commit=True):article=Article(headline='unpublished',published=False)ifcommit:article.save()returnarticledefcreate_article_without_owner(testcase):return{'pk':create_unpublished_article().pk}defcreate_and_return_user_credentials(testcase):user=create_user()return{'username':user.username,'password':'1234'# User contains hashed password only so we should# return it as plain text}defcreate_article_with_its_owner(testcase):owner=create_user()testcase.owner=ownerunpublished=create_unpublished_article(commit=False)unpublished.owner=ownerunpublished.save()return{'pk':unpublished.pk}defget_owner_credentials(testcase):return{'username':testcase.owner.username,'password':'1234'# User contains hashed password only}classUnpublishedArticleSmokeTestCase(SmokeTestCase):TESTS_CONFIGURATION=(('articles:article',404,'GET',{'url_kwargs':create_article_without_owner,'comment':'Anonymous access to unpublished article.'}),# 1('articles:article',404,'GET',{'url_kwargs':create_article_without_owner,'user_credentials':create_and_return_user_credentials,'comment':'Some user access to unpublished article.'}),# 2('articles:article',200,'GET',{'url_kwargs':create_article_with_its_owner,'user_credentials':get_owner_credentials,'comment':'Owner access to unpublished article.'}),# 3)
License
麻省理工学院