1
0
mirror of https://github.com/searxng/searxng.git synced 2024-10-05 19:10:10 +02:00

[mod] move searx/testing.py to the tests directory

move robot tests to tests.robot
manage calls "python -m tests.robot"
This commit is contained in:
Alexandre Flament 2021-09-02 16:01:34 +02:00
parent 065b4dab56
commit b9c73fb697
23 changed files with 161 additions and 157 deletions

2
manage
View File

@ -636,7 +636,7 @@ test.coverage() {
test.robot() { test.robot() {
build_msg TEST 'robot' build_msg TEST 'robot'
gecko.driver gecko.driver
PYTHONPATH=. pyenv.cmd python searx/testing.py robot PYTHONPATH=. pyenv.cmd python -m tests.robot
dump_return $? dump_return $?
} }

View File

@ -1,5 +1,47 @@
import os import os
import aiounittest
os.environ['SEARX_DEBUG'] = '1' os.environ['SEARX_DEBUG'] = '1'
os.environ['SEARX_DISABLE_ETC_SETTINGS'] = '1' os.environ['SEARX_DISABLE_ETC_SETTINGS'] = '1'
os.environ.pop('SEARX_SETTINGS_PATH', None) os.environ.pop('SEARX_SETTINGS_PATH', None)
class SearxTestLayer:
"""Base layer for non-robot tests."""
__name__ = 'SearxTestLayer'
@classmethod
def setUp(cls):
pass
@classmethod
def tearDown(cls):
pass
@classmethod
def testSetUp(cls):
pass
@classmethod
def testTearDown(cls):
pass
class SearxTestCase(aiounittest.AsyncTestCase):
"""Base test case for non-robot tests."""
layer = SearxTestLayer
def setattr4test(self, obj, attr, value):
"""
setattr(obj, attr, value)
but reset to the previous value in the cleanup.
"""
previous_value = getattr(obj, attr)
def cleanup_patch():
setattr(obj, attr, previous_value)
self.addCleanup(cleanup_patch)
setattr(obj, attr, value)

View File

@ -1,76 +0,0 @@
# -*- coding: utf-8 -*-
from time import sleep
url = "http://localhost:11111/"
def test_index(browser):
# Visit URL
browser.visit(url)
assert browser.is_text_present('about')
def test_404(browser):
# Visit URL
browser.visit(url + 'missing_link')
assert browser.is_text_present('Page not found')
def test_about(browser):
browser.visit(url)
browser.click_link_by_text('about')
assert browser.is_text_present('Why use it?')
def test_preferences(browser):
browser.visit(url)
browser.click_link_by_text('preferences')
assert browser.is_text_present('Preferences')
assert browser.is_text_present('Cookies')
assert browser.is_element_present_by_xpath('//label[@for="checkbox_dummy"]')
def test_preferences_engine_select(browser):
browser.visit(url)
browser.click_link_by_text('preferences')
assert browser.is_element_present_by_xpath('//a[@href="#tab_engine"]')
browser.find_by_xpath('//a[@href="#tab_engine"]').first.click()
assert not browser.find_by_xpath('//input[@id="engine_general_dummy__general"]').first.checked
browser.find_by_xpath('//label[@for="engine_general_dummy__general"]').first.check()
browser.find_by_xpath('//input[@value="save"]').first.click()
# waiting for the redirect - without this the test is flaky..
sleep(1)
browser.visit(url)
browser.click_link_by_text('preferences')
browser.find_by_xpath('//a[@href="#tab_engine"]').first.click()
assert browser.find_by_xpath('//input[@id="engine_general_dummy__general"]').first.checked
def test_preferences_locale(browser):
browser.visit(url)
browser.click_link_by_text('preferences')
browser.find_by_xpath('//a[@href="#tab_ui"]').first.click()
browser.select('locale', 'hu')
browser.find_by_xpath('//input[@value="save"]').first.click()
# waiting for the redirect - without this the test is flaky..
sleep(1)
browser.visit(url)
browser.click_link_by_text('beállítások')
browser.is_text_present('Beállítások')
def test_search(browser):
browser.visit(url)
browser.fill('q', 'test search query')
browser.find_by_xpath('//button[@type="submit"]').first.click()
assert browser.is_text_present('didn\'t find any results')

View File

@ -1,40 +1,19 @@
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: AGPL-3.0-or-later # SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint # lint: pylint
"""Shared testing code.""" """Shared testing code."""
# pylint: disable=missing-function-docstring # pylint: disable=missing-function-docstring
import sys
import os import os
import subprocess import subprocess
import traceback import traceback
import pathlib
from os.path import dirname, join, abspath, realpath
from splinter import Browser from splinter import Browser
import aiounittest
import tests as searx_tests
class SearxTestLayer: from tests.robot import test_webapp
"""Base layer for non-robot tests."""
__name__ = 'SearxTestLayer'
@classmethod
def setUp(cls):
pass
@classmethod
def tearDown(cls):
pass
@classmethod
def testSetUp(cls):
pass
@classmethod
def testTearDown(cls):
pass
class SearxRobotLayer(): class SearxRobotLayer():
@ -43,8 +22,10 @@ class SearxRobotLayer():
def setUp(self): def setUp(self):
os.setpgrp() # create new process group, become its leader os.setpgrp() # create new process group, become its leader
tests_path = pathlib.Path(searx_tests.__file__).resolve().parent
# get program paths # get program paths
webapp = join(abspath(dirname(realpath(__file__))), 'webapp.py') webapp = str(tests_path.parent / 'searx' / 'webapp.py')
exe = 'python' exe = 'python'
# The Flask app is started by Flask.run(...), don't enable Flask's debug # The Flask app is started by Flask.run(...), don't enable Flask's debug
@ -57,8 +38,7 @@ class SearxRobotLayer():
os.environ['SEARX_DEBUG'] = '0' os.environ['SEARX_DEBUG'] = '0'
# set robot settings path # set robot settings path
os.environ['SEARX_SETTINGS_PATH'] = abspath( os.environ['SEARX_SETTINGS_PATH'] = str(tests_path / 'robot' / 'settings_robot.yml')
dirname(__file__) + '/settings_robot.yml')
# run the server # run the server
self.server = subprocess.Popen( # pylint: disable=consider-using-with self.server = subprocess.Popen( # pylint: disable=consider-using-with
@ -75,7 +55,6 @@ class SearxRobotLayer():
del os.environ['SEARX_SETTINGS_PATH'] del os.environ['SEARX_SETTINGS_PATH']
# SEARXROBOTLAYER = SearxRobotLayer()
def run_robot_tests(tests): def run_robot_tests(tests):
print('Running {0} tests'.format(len(tests))) print('Running {0} tests'.format(len(tests)))
for test in tests: for test in tests:
@ -83,38 +62,17 @@ def run_robot_tests(tests):
test(browser) test(browser)
class SearxTestCase(aiounittest.AsyncTestCase): def main():
"""Base test case for non-robot tests.""" test_layer = SearxRobotLayer()
try:
layer = SearxTestLayer test_layer.setUp()
run_robot_tests([getattr(test_webapp, x) for x in dir(test_webapp) if x.startswith('test_')])
def setattr4test(self, obj, attr, value): except Exception: # pylint: disable=broad-except
""" print('Error occured: {0}'.format(traceback.format_exc()))
setattr(obj, attr, value) sys.exit(1)
but reset to the previous value in the cleanup. finally:
""" test_layer.tearDown()
previous_value = getattr(obj, attr)
def cleanup_patch():
setattr(obj, attr, previous_value)
self.addCleanup(cleanup_patch)
setattr(obj, attr, value)
if __name__ == '__main__': if __name__ == '__main__':
import sys main()
# test cases
from tests import robot
base_dir = abspath(join(dirname(__file__), '../tests'))
if sys.argv[1] == 'robot':
test_layer = SearxRobotLayer()
errors = False
try:
test_layer.setUp()
run_robot_tests([getattr(robot, x) for x in dir(robot) if x.startswith('test_')])
except Exception: # pylint: disable=broad-except
errors = True
print('Error occured: {0}'.format(traceback.format_exc()))
test_layer.tearDown()
sys.exit(1 if errors else 0)

View File

@ -0,0 +1,78 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
# lint: pylint
# pylint: disable=missing-module-docstring,missing-function-docstring
from time import sleep
url = "http://localhost:11111/"
def test_index(browser):
# Visit URL
browser.visit(url)
assert browser.is_text_present('about')
def test_404(browser):
# Visit URL
browser.visit(url + 'missing_link')
assert browser.is_text_present('Page not found')
def test_about(browser):
browser.visit(url)
browser.click_link_by_text('about')
assert browser.is_text_present('Why use it?')
def test_preferences(browser):
browser.visit(url)
browser.click_link_by_text('preferences')
assert browser.is_text_present('Preferences')
assert browser.is_text_present('Cookies')
assert browser.is_element_present_by_xpath('//label[@for="checkbox_dummy"]')
def test_preferences_engine_select(browser):
browser.visit(url)
browser.click_link_by_text('preferences')
assert browser.is_element_present_by_xpath('//a[@href="#tab_engine"]')
browser.find_by_xpath('//a[@href="#tab_engine"]').first.click()
assert not browser.find_by_xpath('//input[@id="engine_general_dummy__general"]').first.checked
browser.find_by_xpath('//label[@for="engine_general_dummy__general"]').first.check()
browser.find_by_xpath('//input[@value="save"]').first.click()
# waiting for the redirect - without this the test is flaky..
sleep(1)
browser.visit(url)
browser.click_link_by_text('preferences')
browser.find_by_xpath('//a[@href="#tab_engine"]').first.click()
assert browser.find_by_xpath('//input[@id="engine_general_dummy__general"]').first.checked
def test_preferences_locale(browser):
browser.visit(url)
browser.click_link_by_text('preferences')
browser.find_by_xpath('//a[@href="#tab_ui"]').first.click()
browser.select('locale', 'hu')
browser.find_by_xpath('//input[@value="save"]').first.click()
# waiting for the redirect - without this the test is flaky..
sleep(1)
browser.visit(url)
browser.click_link_by_text('beállítások')
browser.is_text_present('Beállítások')
def test_search(browser):
browser.visit(url)
browser.fill('q', 'test search query')
browser.find_by_xpath('//button[@type="submit"]').first.click()
assert browser.is_text_present('didn\'t find any results')

View File

@ -16,7 +16,7 @@ along with searx. If not, see < http://www.gnu.org/licenses/ >.
from searx.engines import command as command_engine from searx.engines import command as command_engine
from searx.testing import SearxTestCase from tests import SearxTestCase
class TestCommandEngine(SearxTestCase): class TestCommandEngine(SearxTestCase):

View File

@ -2,7 +2,7 @@
from collections import defaultdict from collections import defaultdict
import mock import mock
from searx.engines import xpath from searx.engines import xpath
from searx.testing import SearxTestCase from tests import SearxTestCase
class TestXpathEngine(SearxTestCase): class TestXpathEngine(SearxTestCase):

View File

@ -5,7 +5,7 @@ from mock import patch
import httpx import httpx
from searx.network.network import Network, NETWORKS, initialize from searx.network.network import Network, NETWORKS, initialize
from searx.testing import SearxTestCase from tests import SearxTestCase
class TestNetwork(SearxTestCase): class TestNetwork(SearxTestCase):

View File

@ -3,7 +3,7 @@
from mock import Mock from mock import Mock
from searx.answerers import answerers from searx.answerers import answerers
from searx.testing import SearxTestCase from tests import SearxTestCase
class AnswererTest(SearxTestCase): class AnswererTest(SearxTestCase):

View File

@ -1,5 +1,5 @@
from searx.testing import SearxTestCase
from searx import settings, engines from searx import settings, engines
from tests import SearxTestCase
class TestEnginesInit(SearxTestCase): class TestEnginesInit(SearxTestCase):

View File

@ -1,6 +1,6 @@
from searx.external_bang import get_node, resolve_bang_definition, get_bang_url, get_bang_definition_and_autocomplete from searx.external_bang import get_node, resolve_bang_definition, get_bang_url, get_bang_definition_and_autocomplete
from searx.search import SearchQuery, EngineRef from searx.search import SearchQuery, EngineRef
from searx.testing import SearxTestCase from tests import SearxTestCase
TEST_DB = { TEST_DB = {

View File

@ -1,8 +1,8 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from searx.testing import SearxTestCase
from searx import plugins from searx import plugins
from mock import Mock from mock import Mock
from tests import SearxTestCase
def get_search_mock(query, **kwargs): def get_search_mock(query, **kwargs):

View File

@ -1,6 +1,6 @@
from searx.preferences import (EnumStringSetting, MapSetting, MissingArgumentException, SearchLanguageSetting, from searx.preferences import (EnumStringSetting, MapSetting, MissingArgumentException, SearchLanguageSetting,
MultipleChoiceSetting, PluginsSetting, ValidationException) MultipleChoiceSetting, PluginsSetting, ValidationException)
from searx.testing import SearxTestCase from tests import SearxTestCase
class PluginStub: class PluginStub:

View File

@ -1,7 +1,7 @@
from searx import settings from searx import settings
from searx.engines import load_engines from searx.engines import load_engines
from searx.query import RawTextQuery from searx.query import RawTextQuery
from searx.testing import SearxTestCase from tests import SearxTestCase
TEST_ENGINES = [ TEST_ENGINES = [

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from searx.results import ResultContainer from searx.results import ResultContainer
from searx.testing import SearxTestCase from tests import SearxTestCase
def fake_result(url='https://aa.bb/cc?dd=ee#ff', def fake_result(url='https://aa.bb/cc?dd=ee#ff',

View File

@ -1,9 +1,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from searx.testing import SearxTestCase import searx.search
from searx.search import SearchQuery, EngineRef from searx.search import SearchQuery, EngineRef
from searx import settings from searx import settings
import searx.search from tests import SearxTestCase
SAFESEARCH = 0 SAFESEARCH = 0

View File

@ -3,9 +3,9 @@
from os.path import dirname, join, abspath from os.path import dirname, join, abspath
from unittest.mock import patch from unittest.mock import patch
from searx.testing import SearxTestCase
from searx.exceptions import SearxSettingsException from searx.exceptions import SearxSettingsException
from searx import settings_loader from searx import settings_loader
from tests import SearxTestCase
test_dir = abspath(dirname(__file__)) test_dir = abspath(dirname(__file__))

View File

@ -8,8 +8,8 @@ from mock import Mock, patch
from nose2.tools import params from nose2.tools import params
from searx.search import SearchQuery, EngineRef, initialize from searx.search import SearchQuery, EngineRef, initialize
from searx.testing import SearxTestCase
from searx_extra import standalone_searx as sas from searx_extra import standalone_searx as sas
from tests import SearxTestCase
class StandaloneSearx(SearxTestCase): class StandaloneSearx(SearxTestCase):

View File

@ -2,10 +2,11 @@
import lxml.etree import lxml.etree
from lxml import html from lxml import html
from searx.testing import SearxTestCase
from searx.exceptions import SearxXPathSyntaxException, SearxEngineXPathException from searx.exceptions import SearxXPathSyntaxException, SearxEngineXPathException
from searx import utils from searx import utils
from tests import SearxTestCase
class TestUtils(SearxTestCase): class TestUtils(SearxTestCase):

View File

@ -1,12 +1,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from searx.testing import SearxTestCase
from searx.preferences import Preferences from searx.preferences import Preferences
from searx.engines import engines from searx.engines import engines
import searx.search import searx.search
from searx.search import EngineRef from searx.search import EngineRef
from searx.webadapter import validate_engineref_list from searx.webadapter import validate_engineref_list
from tests import SearxTestCase
PRIVATE_ENGINE_NAME = 'general private offline' PRIVATE_ENGINE_NAME = 'general private offline'

View File

@ -3,9 +3,10 @@
import json import json
from urllib.parse import ParseResult from urllib.parse import ParseResult
from mock import Mock from mock import Mock
from searx.testing import SearxTestCase
from searx.search import Search
import searx.search.processors import searx.search.processors
from searx.search import Search
from tests import SearxTestCase
class ViewsTestCase(SearxTestCase): class ViewsTestCase(SearxTestCase):

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import mock import mock
from searx.testing import SearxTestCase
from searx import webutils from searx import webutils
from tests import SearxTestCase
class TestWebUtils(SearxTestCase): class TestWebUtils(SearxTestCase):