Search code examples
unit-testingplonezopeutilitygrok

Utility not found when run bin/test in plone 5


In plone 5, I create an interface named IFindMathModeTexInText and register a global utility using five.grok as follow:

class FindMathModeTexInText (grok.GlobalUtility):
    implements (IFindMathModeTexInText)
    def process(self, text):
        equation_indices, all_indices = find_equation(text)
        return create_list_of_text_nodes(text, equation_indices, all_indices)

I called IFindMathModeTexInText in different module using zope.component.getUtility as follow:

result = getUtility(IFindMathModeTexInText).process(new_el_text)

I have no trouble when running bin/instance fg However when I run bin/test I got the following error:

in getUtility
    raise ComponentLookupError(interface, name)
ComponentLookupError: (<InterfaceClass nti.content.util.common_interfaces.IFindMathModeTexInText>, '')

I understand that if no utility can be found, a ComponentLookupError will be raised. Why is this error raised when I run bin.test while when I run bin/instance fg, the utility is found.

ps : the complete traceback is as follow:

Error in test test_html_header (nti.content.tools.tests.test_html_to_latex.TestHTMLToLatex)
Traceback (most recent call last):
  File "/Users/ega/CCMF/PloneCourseContentCMF/PloneCourseContentCMF-Buildout/buildout-cache/eggs/unittest2-0.5.1-py2.7.egg/unittest2/case.py", line 340, in run
    testMethod()
  File "/Users/ega/CCMF/PloneCourseContentCMF/PloneCourseContentCMF-Buildout/sources/nti.content/nti/content/tools/tests/test_html_to_latex.py", line 31, in test_html_header
    node = RichText.process(script, reading_type = True)
  File "/Users/ega/CCMF/PloneCourseContentCMF/PloneCourseContentCMF-Buildout/sources/nti.content/nti/content/tools/html_adapter/rich_text_adapter.py", line 21, in process
    me.add_child(Run.process(element,[],reading_type))
  File "/Users/ega/CCMF/PloneCourseContentCMF/PloneCourseContentCMF-Buildout/sources/nti.content/nti/content/tools/html_adapter/run_html_adapter.py", line 25, in process
    me = check_child(me, element, reading_type)
  File "/Users/ega/CCMF/PloneCourseContentCMF/PloneCourseContentCMF-Buildout/sources/nti.content/nti/content/tools/html_adapter/run_html_adapter.py", line 322, in check_child
    me.add_child(_process_h1_elements(child,reading_type))
  File "/Users/ega/CCMF/PloneCourseContentCMF/PloneCourseContentCMF-Buildout/sources/nti.content/nti/content/tools/html_adapter/run_html_adapter.py", line 393, in _process_h1_elements
    return Paragraph.process(element, ['Heading1'], reading_type)
  File "/Users/ega/CCMF/PloneCourseContentCMF/PloneCourseContentCMF-Buildout/sources/nti.content/nti/content/tools/html_adapter/run_html_adapter.py", line 39, in process
    me = check_element_text(me, element)
  File "/Users/ega/CCMF/PloneCourseContentCMF/PloneCourseContentCMF-Buildout/sources/nti.content/nti/content/tools/html_adapter/run_html_adapter.py", line 287, in check_element_text
    list_of_child_nodes = getUtility(IFindMathModeTexInText).process(new_el_text)
  File "/Users/ega/CCMF/PloneCourseContentCMF/PloneCourseContentCMF-Buildout/buildout-cache/eggs/zope.component-3.9.5-py2.7.egg/zope/component/_api.py", line 169, in getUtility
    raise ComponentLookupError(interface, name)
ComponentLookupError: (<InterfaceClass nti.content.util.common_interfaces.IFindMathModeTexInText>, '')

Thanks


Solution

  • I solved the problem by defining layer of unit-test as documented in the plone.app.testing documentation.