How to use invalid_template method in pytest-django

Best Python code snippet using pytest-django_python

test_environment.py

Source:test_environment.py Github

copy

Full Screen

...51 )52 django_testdir.create_app_file(53 """54 from django.shortcuts import render55 def invalid_template(request):56 return render(request, 'invalid_template.html', {})57 """,58 "views.py",59 )60 django_testdir.create_app_file(61 "<div>{{ invalid_var }}</div>", "templates/invalid_template_base.html"62 )63 django_testdir.create_app_file(64 "{% include 'invalid_template_base.html' %}", "templates/invalid_template.html"65 )66 django_testdir.create_test_module(67 """68 import pytest69 def test_for_invalid_template(client):70 client.get('/invalid_template/')71 @pytest.mark.ignore_template_errors72 def test_ignore(client):73 client.get('/invalid_template/')74 """75 )76 result = django_testdir.runpytest_subprocess("-s", "--fail-on-template-vars")77 if get_django_version() >= (1, 9):78 origin = "'*/tpkg/app/templates/invalid_template_base.html'"79 else:80 origin = "'invalid_template.html'"81 result.stdout.fnmatch_lines_random(82 [83 "tpkg/test_the_test.py F.*",84 "E * Failed: Undefined template variable 'invalid_var' in {}".format(85 origin86 ),87 ]88 )89@pytest.mark.django_project(90 extra_settings="""91 TEMPLATE_LOADERS = (92 'django.template.loaders.filesystem.Loader',93 'django.template.loaders.app_directories.Loader',94 )95 ROOT_URLCONF = 'tpkg.app.urls'96 """97)98def test_invalid_template_with_default_if_none(django_testdir):99 django_testdir.create_app_file(100 """101 <div>{{ data.empty|default:'d' }}</div>102 <div>{{ data.none|default:'d' }}</div>103 <div>{{ data.empty|default_if_none:'d' }}</div>104 <div>{{ data.none|default_if_none:'d' }}</div>105 <div>{{ data.missing|default_if_none:'d' }}</div>106 """,107 "templates/the_template.html",108 )109 django_testdir.create_test_module(110 """111 def test_for_invalid_template():112 from django.shortcuts import render113 render(114 request=None,115 template_name='the_template.html',116 context={'data': {'empty': '', 'none': None}},117 )118 """119 )120 result = django_testdir.runpytest_subprocess("--fail-on-template-vars")121 result.stdout.fnmatch_lines(122 [123 "tpkg/test_the_test.py F",124 "E * Failed: Undefined template variable 'data.missing' in *the_template.html'",125 ]126 )127@pytest.mark.django_project(128 extra_settings="""129 TEMPLATE_LOADERS = (130 'django.template.loaders.filesystem.Loader',131 'django.template.loaders.app_directories.Loader',132 )133 ROOT_URLCONF = 'tpkg.app.urls'134 """135)136def test_invalid_template_variable_opt_in(django_testdir):137 django_testdir.create_app_file(138 """139 from django.conf.urls import url140 from tpkg.app import views141 urlpatterns = [url(r'invalid_template/', views.invalid_template)]142 """,143 "urls.py",144 )145 django_testdir.create_app_file(146 """147 from django.shortcuts import render148 def invalid_template(request):149 return render(request, 'invalid_template.html', {})150 """,151 "views.py",152 )153 django_testdir.create_app_file(154 "<div>{{ invalid_var }}</div>", "templates/invalid_template.html"155 )156 django_testdir.create_test_module(157 """158 import pytest159 def test_for_invalid_template(client):160 client.get('/invalid_template/')161 @pytest.mark.ignore_template_errors162 def test_ignore(client):163 client.get('/invalid_template/')164 """165 )166 result = django_testdir.runpytest_subprocess("-s")167 result.stdout.fnmatch_lines_random(["tpkg/test_the_test.py ..*"])168@pytest.mark.django_db169def test_database_rollback():170 assert Item.objects.count() == 0171 Item.objects.create(name="blah")172 assert Item.objects.count() == 1173@pytest.mark.django_db...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pytest-django automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful