Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9177805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:24:35+00:00 2026-06-17T17:24:35+00:00

I encountered a problem with unit tests when use i18 in my project. My

  • 0

I encountered a problem with unit tests when use i18 in my project.
My project uses framewoks i18 and webapp2
The function uses the translation by i18. But when I test, I get the error – missing global variable request.
For example it is:

from unittest import TestCase, main
from webapp2_extras.i18n import lazy_gettext as _

def Hello(a):
    if a > 0:
       message = _('My great message!11 a > 0')
    else:
       message = _('My great message!11 a =< 0')
    return message

class TestHandler(TestCase):

    def testHello0(self):
        self.assertEqual(Hello(0), 'My great message!11 a =< 0')

    def testHello3(self):
        self.assertEqual(Hello(3), 'My great message!11 a > 0')

if __name__ == '__main__':
     main()

and I have message:

FAIL: testHello0 (text3.TestHandler)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/text3.py", line 14, in testHello0
    self.assertEqual(Hello(0), 'My great message!11 a =< 0')
AssertionError: Request global variable is not set.

I have a solution to this problem, but I do not like it. This is a crutch. How can you test my function without using framewoks on similarity webapp2

from unittest import TestCase, main
from webapp2_extras.i18n import lazy_gettext as _
import webapp2

def Hello(a):
    if a > 0:
        message = _('My great message!11 a > 0')
    else:
        message = _('My great message!11 a =< 0')
    return message


class OneHandler(webapp2.RequestHandler):
    def get(self):
        myNumber = self.request.get('myNumber')
        myNumber = int(myNumber)
        message = Hello(myNumber)
        self.response.write(message)


routes = [('/One', OneHandler)]
app = webapp2.WSGIApplication(routes = routes)

class TestHandler(TestCase):

    def testHello0(self):
        myNumber = 0
        URL = '/One?myNumber=%s' % myNumber

        self.response = app.get_response(URL)
        self.mess = self.response.body
        self.assertEqual(self.mess, 'My great message!11 a =< 0') 

    def testHello3(self):
        myNumber = 3
        URL = '/One?myNumber=%s' % myNumber

        self.response = app.get_response(URL)
        self.mess = self.response.body
        self.assertEqual(self.mess, 'My great message!11 a > 0') 


if __name__ == '__main__':
    main()

and well done!

Ran 2 tests in 0.047s

OK

But it's not a good solution. How I can testing my function and dont use webapp2, etc?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:24:37+00:00Added an answer on June 17, 2026 at 5:24 pm

    just don’t test external code and patch your _lazy function

    from unittest import TestCase
    import fudge
    
    from path.to import fun
    # def fun(msg):
    #     return _lazy("foo %s") % msg
    
    class TestHandler(TestCase):
        @fudge.patch("path.to._lazy")
        def test_foo(self, fake_lazy)
            fake_lazy.is_callable().calls(lambda s, **kw: s)
            self.assertEqual(fun("bar"), "foo bar")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I encountered a problem when trying to test a module with Test::Unit. What I
Encountered this problem before but forgot how I solved it. I want to use
I encountered a problem when I tried to use java from matlab. I read
Problem: When I add UpdateCacheMiddleware and FetchFromCacheMiddleware to my Django project, I get unittest
I encountered a problem here. I'm using C++ multiset. This is the test file.
I have encountered a problem where when i use any theme in the android
I encountered a problem in c++. I read some codes,but there was a very
Problem Encountered At runtime, I always get the following NHibernate.MappingException : Could not compile
I encountered this problem when updating my background to use a 9-patch image. The
I encountered similar problem if statement vs OO Design - 1 but it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.