How to use create_app_file method in pytest-django

Best Python code snippet using pytest-django_python

test_environment.py

Source:test_environment.py Github

copy

Full Screen

...38 ROOT_URLCONF = 'tpkg.app.urls'39 """40)41def test_invalid_template_variable(django_testdir):42 django_testdir.create_app_file(43 """44 from django.urls import path45 from tpkg.app import views46 urlpatterns = [path('invalid_template/', views.invalid_template)]47 """,48 "urls.py",49 )50 django_testdir.create_app_file(51 """52 from django.shortcuts import render53 def invalid_template(request):54 return render(request, 'invalid_template.html', {})55 """,56 "views.py",57 )58 django_testdir.create_app_file(59 "<div>{{ invalid_var }}</div>", "templates/invalid_template_base.html"60 )61 django_testdir.create_app_file(62 "{% include 'invalid_template_base.html' %}", "templates/invalid_template.html"63 )64 django_testdir.create_test_module(65 """66 import pytest67 def test_for_invalid_template(client):68 client.get('/invalid_template/')69 @pytest.mark.ignore_template_errors70 def test_ignore(client):71 client.get('/invalid_template/')72 """73 )74 result = django_testdir.runpytest_subprocess("-s", "--fail-on-template-vars")75 origin = "'*/tpkg/app/templates/invalid_template_base.html'"76 result.stdout.fnmatch_lines_random(77 [78 "tpkg/test_the_test.py F.*",79 "E * Failed: Undefined template variable 'invalid_var' in {}".format(80 origin81 ),82 ]83 )84@pytest.mark.django_project(85 extra_settings="""86 TEMPLATE_LOADERS = (87 'django.template.loaders.filesystem.Loader',88 'django.template.loaders.app_directories.Loader',89 )90 ROOT_URLCONF = 'tpkg.app.urls'91 """92)93def test_invalid_template_with_default_if_none(django_testdir):94 django_testdir.create_app_file(95 """96 <div>{{ data.empty|default:'d' }}</div>97 <div>{{ data.none|default:'d' }}</div>98 <div>{{ data.empty|default_if_none:'d' }}</div>99 <div>{{ data.none|default_if_none:'d' }}</div>100 <div>{{ data.missing|default_if_none:'d' }}</div>101 """,102 "templates/the_template.html",103 )104 django_testdir.create_test_module(105 """106 def test_for_invalid_template():107 from django.shortcuts import render108 render(109 request=None,110 template_name='the_template.html',111 context={'data': {'empty': '', 'none': None}},112 )113 """114 )115 result = django_testdir.runpytest_subprocess("--fail-on-template-vars")116 result.stdout.fnmatch_lines(117 [118 "tpkg/test_the_test.py F",119 "E * Failed: Undefined template variable 'data.missing' in *the_template.html'",120 ]121 )122@pytest.mark.django_project(123 extra_settings="""124 TEMPLATE_LOADERS = (125 'django.template.loaders.filesystem.Loader',126 'django.template.loaders.app_directories.Loader',127 )128 ROOT_URLCONF = 'tpkg.app.urls'129 """130)131def test_invalid_template_variable_opt_in(django_testdir):132 django_testdir.create_app_file(133 """134 from django.urls import path135 from tpkg.app import views136 urlpatterns = [path('invalid_template', views.invalid_template)]137 """,138 "urls.py",139 )140 django_testdir.create_app_file(141 """142 from django.shortcuts import render143 def invalid_template(request):144 return render(request, 'invalid_template.html', {})145 """,146 "views.py",147 )148 django_testdir.create_app_file(149 "<div>{{ invalid_var }}</div>", "templates/invalid_template.html"150 )151 django_testdir.create_test_module(152 """153 import pytest154 def test_for_invalid_template(client):155 client.get('/invalid_template/')156 @pytest.mark.ignore_template_errors157 def test_ignore(client):158 client.get('/invalid_template/')159 """160 )161 result = django_testdir.runpytest_subprocess("-s")162 result.stdout.fnmatch_lines_random(["tpkg/test_the_test.py ..*"])...

Full Screen

Full Screen

app.py

Source:app.py Github

copy

Full Screen

...6import re7from pathlib import Path8import sys9APP_PATTERN = re.compile(r'data-app-name="\w+"')10def create_app_file(input_file, output_file, app_name):11 # Read in the file12 with open(input_file, "r") as f_in:13 content = f_in.read()14 patched_content = APP_PATTERN.sub(f'data-app-name="{app_name}"', content)15 with open(output_file, "w") as f_out:16 f_out.write(patched_content)17def main():18 parser = argparse.ArgumentParser(19 description="HTML app file generator for trame applications"20 )21 parser.add_argument(22 "--name",23 default="trame",24 help="Application name to encode inside HTML {name}.html",25 )26 parser.add_argument(27 "--input",28 help="Input file to use as template",29 )30 args, _ = parser.parse_known_args()31 # Handle input32 input_file = Path(args.input)33 if not input_file.exists():34 parser.print_help()35 sys.exit(0)36 if input_file.is_dir():37 input_file = input_file / "index.html"38 if not input_file.exists():39 parser.print_help()40 sys.exit(0)41 output_file = input_file.with_stem(args.name)42 create_app_file(input_file, output_file, args.name)43if __name__ == "__main__":...

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