Best Python code snippet using hypothesis
integration_test.py
Source:integration_test.py
1#!/usr/bin/env python2# Copyright 2013 The Chromium Authors. All rights reserved.3# Use of this source code is governed by a BSD-style license that can be4# found in the LICENSE file.5# Run build_server so that files needed by tests are copied to the local6# third_party directory.7import build_server8build_server.main()9import json10import optparse11import os12import posixpath13import sys14import time15import unittest16from branch_utility import BranchUtility17from chroot_file_system import ChrootFileSystem18from extensions_paths import (19 CONTENT_PROVIDERS, CHROME_EXTENSIONS, PUBLIC_TEMPLATES)20from fake_fetchers import ConfigureFakeFetchers21from special_paths import SITE_VERIFICATION_FILE22from handler import Handler23from link_error_detector import LinkErrorDetector, StringifyBrokenLinks24from local_file_system import LocalFileSystem25from local_renderer import LocalRenderer26from path_util import AssertIsValid27from servlet import Request28from third_party.json_schema_compiler import json_parse29from test_util import (30 ChromiumPath, DisableLogging, EnableLogging, ReadFile, Server2Path)31# Arguments set up if __main__ specifies them.32_EXPLICIT_TEST_FILES = None33_REBASE = False34_VERBOSE = False35def _ToPosixPath(os_path):36 return os_path.replace(os.sep, '/')37def _FilterHidden(paths):38 '''Returns a list of the non-hidden paths from |paths|.39 '''40 # Hidden files start with a '.' but paths like './foo' and '../foo' are not41 # hidden.42 return [path for path in paths if (not path.startswith('.')) or43 path.startswith('./') or44 path.startswith('../')]45def _GetPublicFiles():46 '''Gets all public file paths mapped to their contents.47 '''48 def walk(path, prefix=''):49 path = ChromiumPath(path)50 public_files = {}51 for root, dirs, files in os.walk(path, topdown=True):52 relative_root = root[len(path):].lstrip(os.path.sep)53 dirs[:] = _FilterHidden(dirs)54 for filename in _FilterHidden(files):55 with open(os.path.join(root, filename), 'r') as f:56 request_path = posixpath.join(prefix, relative_root, filename)57 public_files[request_path] = f.read()58 return public_files59 # Public file locations are defined in content_providers.json, sort of. Epic60 # hack to pull them out; list all the files from the directories that61 # Chromium content providers ask for.62 public_files = {}63 content_providers = json_parse.Parse(ReadFile(CONTENT_PROVIDERS))64 for content_provider in content_providers.itervalues():65 if 'chromium' in content_provider:66 public_files.update(walk(content_provider['chromium']['dir'],67 prefix=content_provider['serveFrom']))68 return public_files69class IntegrationTest(unittest.TestCase):70 def setUp(self):71 ConfigureFakeFetchers()72 @EnableLogging('info')73 def testCronAndPublicFiles(self):74 '''Runs cron then requests every public file. Cron needs to be run first75 because the public file requests are offline.76 '''77 if _EXPLICIT_TEST_FILES is not None:78 return79 print('Running cron...')80 start_time = time.time()81 try:82 response = Handler(Request.ForTest('/_cron')).Get()83 if response:84 self.assertEqual(200, response.status)85 self.assertEqual('Success', response.content.ToString())86 else:87 self.fail('No response for _cron')88 finally:89 print('Took %s seconds' % (time.time() - start_time))90 # TODO(kalman): Re-enable this, but it takes about an hour at the moment,91 # presumably because every page now has a lot of links on it from the92 # topnav.93 #print("Checking for broken links...")94 #start_time = time.time()95 #link_error_detector = LinkErrorDetector(96 # # TODO(kalman): Use of ChrootFileSystem here indicates a hack. Fix.97 # ChrootFileSystem(LocalFileSystem.Create(), CHROME_EXTENSIONS),98 # lambda path: Handler(Request.ForTest(path)).Get(),99 # 'templates/public',100 # ('extensions/index.html', 'apps/about_apps.html'))101 #broken_links = link_error_detector.GetBrokenLinks()102 #if broken_links:103 # print('Found %d broken links.' % (104 # len(broken_links)))105 # if _VERBOSE:106 # print(StringifyBrokenLinks(broken_links))107 #broken_links_set = set(broken_links)108 #known_broken_links_path = os.path.join(109 # Server2Path('known_broken_links.json'))110 #try:111 # with open(known_broken_links_path, 'r') as f:112 # # The JSON file converts tuples and sets into lists, and for this113 # # set union/difference logic they need to be converted back.114 # known_broken_links = set(tuple(item) for item in json.load(f))115 #except IOError:116 # known_broken_links = set()117 #newly_broken_links = broken_links_set - known_broken_links118 #fixed_links = known_broken_links - broken_links_set119 #print('Took %s seconds.' % (time.time() - start_time))120 #print('Searching for orphaned pages...')121 #start_time = time.time()122 #orphaned_pages = link_error_detector.GetOrphanedPages()123 #if orphaned_pages:124 # # TODO(jshumway): Test should fail when orphaned pages are detected.125 # print('Found %d orphaned pages:' % len(orphaned_pages))126 # for page in orphaned_pages:127 # print(page)128 #print('Took %s seconds.' % (time.time() - start_time))129 public_files = _GetPublicFiles()130 print('Rendering %s public files...' % len(public_files.keys()))131 start_time = time.time()132 try:133 for path, content in public_files.iteritems():134 AssertIsValid(path)135 if path.endswith('redirects.json'):136 continue137 # The non-example html and md files are served without their file138 # extensions.139 path_without_ext, ext = posixpath.splitext(path)140 if (ext in ('.html', '.md') and141 '/examples/' not in path and142 path != SITE_VERIFICATION_FILE):143 path = path_without_ext144 def check_result(response):145 self.assertEqual(200, response.status,146 'Got %s when rendering %s' % (response.status, path))147 # This is reaaaaally rough since usually these will be tiny templates148 # that render large files. At least it'll catch zero-length responses.149 self.assertTrue(len(response.content) >= len(content),150 'Rendered content length was %s vs template content length %s '151 'when rendering %s' % (len(response.content), len(content), path))152 check_result(Handler(Request.ForTest(path)).Get())153 if path.startswith(('apps/', 'extensions/')):154 # Make sure that adding the .html will temporarily redirect to155 # the path without the .html for APIs and articles.156 if '/examples/' not in path:157 redirect_response = Handler(Request.ForTest(path + '.html')).Get()158 self.assertEqual(159 ('/' + path, False), redirect_response.GetRedirect(),160 '%s.html did not (temporarily) redirect to %s (status %s)' %161 (path, path, redirect_response.status))162 # Make sure including a channel will permanently redirect to the same163 # path without a channel.164 for channel in BranchUtility.GetAllChannelNames():165 redirect_response = Handler(166 Request.ForTest(posixpath.join(channel, path))).Get()167 self.assertEqual(168 ('/' + path, True),169 redirect_response.GetRedirect(),170 '%s/%s did not (permanently) redirect to %s (status %s)' %171 (channel, path, path, redirect_response.status))172 # Samples are internationalized, test some locales.173 if path.endswith('/samples'):174 for lang in ('en-US', 'es', 'ar'):175 check_result(Handler(Request.ForTest(176 path,177 headers={'Accept-Language': '%s;q=0.8' % lang})).Get())178 finally:179 print('Took %s seconds' % (time.time() - start_time))180 #if _REBASE:181 # print('Rebasing broken links with %s newly broken and %s fixed links.' %182 # (len(newly_broken_links), len(fixed_links)))183 # with open(known_broken_links_path, 'w') as f:184 # json.dump(broken_links, f,185 # indent=2, separators=(',', ': '), sort_keys=True)186 #else:187 # if fixed_links or newly_broken_links:188 # print('**********************************************\n'189 # 'CHANGE DETECTED IN BROKEN LINKS WITHOUT REBASE\n'190 # '**********************************************')191 # print('Found %s broken links, and some have changed. '192 # 'If this is acceptable or expected then run %s with the --rebase '193 # 'option.' % (len(broken_links), os.path.split(__file__)[-1]))194 # elif broken_links:195 # print('%s existing broken links' % len(broken_links))196 # if fixed_links:197 # print('%s broken links have been fixed:' % len(fixed_links))198 # print(StringifyBrokenLinks(fixed_links))199 # if newly_broken_links:200 # print('There are %s new broken links:' % len(newly_broken_links))201 # print(StringifyBrokenLinks(newly_broken_links))202 # self.fail('See logging for details.')203 # TODO(kalman): Move this test elsewhere, it's not an integration test.204 # Perhaps like "presubmit_tests" or something.205 def testExplicitFiles(self):206 '''Tests just the files in _EXPLICIT_TEST_FILES.207 '''208 if _EXPLICIT_TEST_FILES is None:209 return210 for filename in _EXPLICIT_TEST_FILES:211 print('Rendering %s...' % filename)212 start_time = time.time()213 try:214 response = LocalRenderer.Render(_ToPosixPath(filename))215 self.assertEqual(200, response.status)216 self.assertTrue(response.content != '')217 finally:218 print('Took %s seconds' % (time.time() - start_time))219 # TODO(jshumway): Check page for broken links (currently prohibited by the220 # time it takes to render the pages).221 @DisableLogging('warning')222 def testFileNotFound(self):223 response = LocalRenderer.Render('/extensions/notfound')224 self.assertEqual(404, response.status)225 def testSiteVerificationFile(self):226 response = LocalRenderer.Render('/' + SITE_VERIFICATION_FILE)227 self.assertEqual(200, response.status)228if __name__ == '__main__':229 parser = optparse.OptionParser()230 parser.add_option('-a', '--all', action='store_true', default=False,231 help='Render all pages, not just the one specified')232 parser.add_option('-r', '--rebase', action='store_true', default=False,233 help='Rewrites the known_broken_links.json file with '234 'the current set of broken links')235 parser.add_option('-v', '--verbose', action='store_true', default=False,236 help='Show verbose output like currently broken links')237 (opts, args) = parser.parse_args()238 if not opts.all:239 _EXPLICIT_TEST_FILES = args240 _REBASE = opts.rebase241 _VERBOSE = opts.verbose242 # Kill sys.argv because we have our own flags.243 sys.argv = [sys.argv[0]]...
test_broken_links.py
Source:test_broken_links.py
1#!/usr/bin/env python2# Licensed to the Apache Software Foundation (ASF) under one3# or more contributor license agreements. See the NOTICE file4# distributed with this work for additional information5# regarding copyright ownership. The ASF licenses this file6# to you under the Apache License, Version 2.0 (the7# "License"); you may not use this file except in compliance8# with the License. You may obtain a copy of the License at9#10# http://www.apache.org/licenses/LICENSE-2.011#12# Unless required by applicable law or agreed to in writing,13# software distributed under the License is distributed on an14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15# KIND, either express or implied. See the License for the16# specific language governing permissions and limitations17# under the License.18import sys19from subprocess import check_output, STDOUT, CalledProcessError20def prepare_link_test_result(command_output):21 # Constant string patterns22 NEW_PAGE_TEST_START_REGEX = "Getting links from:"23 BROKEN_PAGE_START_REGEX = "BROKEN"24 PAGE_TEST_END_REGEX = "Finished! "25 # Whitelisted broken links patterns26 HTTP_403_REGEX = "(HTTP_403)"27 HTTP_401_REGEX = "(HTTP_401)"28 BLC_UNKNOWN_REGEX = "(BLC_UNKNOWN)"29 HTTP_UNDEFINED = "HTTP_undefined"30 FALSE_SCALA_API_DOC_LINK = "java$lang.html"31 FALSE_SCALA_API_DEPRECATED_LINK = "api/scala/docs/deprecated-list.html"32 FALSE_PAPER_LINK = "https://static.googleusercontent.com/media/research.google.com/en/pubs/archive/45488.pdf"33 # Initialize flags with happy case34 current_page = ""35 current_page_broken = False36 current_page_broken_links = ""37 broken_links_count = 038 broken_links_summary = ""39 for line in command_output.splitlines():40 if line.startswith(NEW_PAGE_TEST_START_REGEX):41 # New page test is starting. Reset the flags42 current_page = line.split(NEW_PAGE_TEST_START_REGEX)[1]43 current_page_broken = False44 current_page_broken_links = ""45 if line.find(BROKEN_PAGE_START_REGEX) != -1:46 # Skip (401, 403, unknown issues)47 if HTTP_403_REGEX not in line and HTTP_401_REGEX not in line and BLC_UNKNOWN_REGEX not in line and HTTP_UNDEFINED not in line and FALSE_SCALA_API_DOC_LINK not in line and FALSE_SCALA_API_DEPRECATED_LINK not in line and FALSE_PAPER_LINK not in line:48 current_page_broken = True49 current_page_broken_links += line.split(BROKEN_PAGE_START_REGEX)[1] + "\n"50 if line.startswith(PAGE_TEST_END_REGEX):51 if current_page_broken:52 broken_links_count += 153 broken_links_summary += "\nURL - " + current_page54 broken_links_summary += "\nBroken Links\n" + current_page_broken_links55 return broken_links_count, broken_links_summary56# Command to check broken links57# Reference - https://www.npmjs.com/package/broken-link-checker58cmd = "blc https://mxnet.incubator.apache.org -ro"59broken_links_count = 060broken_links_summary = ""61text_file = open("./blc_output.txt", 'w')62command_output = ""63print("Starting broken link test with command $ " + cmd)64try:65 command_output = check_output(cmd, stderr=STDOUT, shell=True)66except CalledProcessError as ex:67 if ex.returncode > 1:68 print("Failed to do broken link test. Console output : \n" + ex.output)69 sys.exit(ex.returncode)70 command_output = ex.output71text_file.write(command_output)72text_file.close()73broken_links_count, broken_links_summary = prepare_link_test_result(command_output)74# These START and END string in output is used to parse the script output in automated scripts and nightly jobs.75print("START - Broken links count")76print(broken_links_count)77print("END - Broken links count")78print("START - Broken links summary")79if broken_links_count == 0:80 print("No broken links in https://mxnet.incubator.apache.org")81 print("END - Broken links summary")82else:83 print(broken_links_summary)...
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!