Skip to content

Commit

Permalink
[fix] unit test_xpath.py: name 'logger' is not defined
Browse files Browse the repository at this point in the history
Depending on the order in which the unit tests are executed, the python modules
of the engines are initialized (monkey patched) or not. As the order of the
tests is not static, random errors may occur.

To avaoid random `NameError: name 'logger' is not defined` in the unit tests of
the xpath engine, a logger is monkey patched into the xpath py-module.

```
make test.unit
TEST      tests/unit
......EE...................
======================================================================
ERROR: test_response (tests.unit.engines.test_xpath.TestXpathEngine.test_response)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./tests/unit/engines/test_xpath.py", line 60, in test_response
    self.assertEqual(xpath.response(response), [])
                     ^^^^^^^^^^^^^^^^^^^^^^^^
  File "./searx/engines/xpath.py", line 309, in response
    logger.debug("found %s results", len(results))
    ^^^^^^
NameError: name 'logger' is not defined

======================================================================
ERROR: test_response_results_xpath (tests.unit.engines.test_xpath.TestXpathEngine.test_response_results_xpath)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./tests/unit/engines/test_xpath.py", line 102, in test_response_results_xpath
    self.assertEqual(xpath.response(response), [])
                     ^^^^^^^^^^^^^^^^^^^^^^^^
  File "./searx/engines/xpath.py", line 309, in response
    logger.debug("found %s results", len(results))
    ^^^^^^
NameError: name 'logger' is not defined
```

Signed-off-by: Markus Heiser <[email protected]>
  • Loading branch information
return42 committed Jun 28, 2024
1 parent a5f8e08 commit d80fcbc
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/unit/engines/test_xpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@

from collections import defaultdict
import mock

from searx.engines import xpath
from searx import logger

from tests import SearxTestCase

logger = logger.getChild('engines')


class TestXpathEngine(SearxTestCase): # pylint: disable=missing-class-docstring
html = """
Expand All @@ -23,6 +28,9 @@ class TestXpathEngine(SearxTestCase): # pylint: disable=missing-class-docstring
</div>
"""

def setUp(self):
xpath.logger = logger.getChild('test_xpath')

def test_request(self):
xpath.search_url = 'https://url.com/{query}'
xpath.categories = []
Expand Down

0 comments on commit d80fcbc

Please sign in to comment.