Package: django-skd-smoke
This package is intended for simplification of smoke tests creation.ContentsInstallationUsageConfigurationExamplesLicenseInstallationYou can get django-skd-smoke by using pip:$ pip install django-skd-smokeUsageAfter installation you should create newTestCasederived fromskd_smoke.SmokeTestCaseand define your smoke tests configuration.
Please reviewExamplessection which demonstrates different usecases.
They are related toexample_projectdirectory which contains common
django project.ConfigurationTESTS_CONFIGURATIONof yourTestCaseshould contain tuple/list of
tuples for every request with the next structure:(url,status,method,{'comment':None,'initialize':None,'url_kwargs':None,'request_data':None,'user_credentials':None,'redirect_to':None})ParameterDescriptionRequiredurlplain url or urlname as stringYesstatusexpected http status code (200, 404, etc.) asintYesmethodhttp request method (GET, POST, etc.) as stringYescommentstring which is added to__doc__of generated test methodNoinitializecallable object to do any required initializationNourl_argslist or callable object which returns args list to resolve url usingdjango.shortcuts.resolve_urlNourl_kwargsdict or callable object which returns kwargs dict to resolve url usingdjango.shortcuts.resolve_urlNorequest_datadict or callable object which returns dict to pass it into http method requestNouser_credentialsdict or callable object which returns dict to login user usingdjango.test.TestCase.client.loginNoredirect_toplain url as string which is checked if only status is one of the next: 301, 302, 303, 307NoNOTE!All callables take yourTestCaseas the first argument so
you can use it to transfer state between them. But take into account thatorder of callbacks usageis next:initializeurl_kwargsuser_credentialsrequest_dataExamplesAll examples are taken fromexample_projectpackage and can be run after
repository cloning.1. Demonstration of simple requests:GET 200GET 200 with request_data as dictPOST 200POST 302 with request_data as callableGET 302 (unauthorized access)GET 200 (authorized access)POST 405 (method not allowed)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. Usage ofinitializecallback to create several objects to test objects
list.Suppose you want to make smoke test for articles list page but initially your
test db does not contain any. You can useinitializecallback here to
create several articles.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),)3. Usage ofredirect_tosetting to test anonymous access of login required
pages.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'}),)4. Usage ofurl_kwargsanduser_credentialscallbacks to test
authorized access of owner to newly created object.Suppose you have a model Article which unpublished version can be viewed by
its owner only. You can test this situation by creating of user inurl_kwargscallback and transfering user touser_credentialscallback.
Unfortunately, you cannot get password from user model cause it contains
hashed password. So you should return password as plain text.Lets smoke test two other situations when 404 page is showed. Finally we have
three testcases:Anonymous access should show 404 page.Some ordinary user access should also show 404 page.Only owner access returns actual article with status 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)LicenseMIT