[Tutor] Unittest

Mark Tolonen metolone+gmane at gmail.com
Thu Jul 17 08:55:26 CEST 2008


OK, your console is set to 'ascii' ('cp437' was my example and is the
Windows console encoding).  'ascii' won't be able to display Russian.
It shouldn't have displayed the "ИзвениÑ" characters either.
Are you still running on the same terminal that display those
characters?  Can you change your terminals encoding preference via an
environment variable?
--
Mark
  "Oleg Oltar" <oltarasenko at gmail.com> wrote in message news:b4fc2ad80807162333k6badc3d3of87f402003a3a00a at mail.gmail.com...

  And in case:
  # coding: utf-8


    import traceback 
    try:
        raise Exception(u'Зрегиться')
    except Exception,e:
        print traceback.format_exc().decode('utf-8').encode('cp437', 'replace')


  Getting

  beryl:~ oleg$ python ./wish/newaccount/reg.py
  Traceback (most recent call last):
    File "./wish/newaccount/reg.py", line 5, in <module>
      raise Exception(u'?????????')
  Exception: <unprintable Exception object>



  My console settings:

  In [1]: import sys

  In [2]: sys.getdefaultencoding()
  Out[2]: 'ascii'

  In [3]: sys.stdout.encoding
  Out[3]: 'US-ASCII'



  On Thu, Jul 17, 2008 at 9:30 AM, Oleg Oltar <oltarasenko at gmail.com> wrote:

    OK
    the output:


      # coding: utf-8

      import traceback 
      try:

          raise Exception(u'Зрегиться')

      except Exception,e:
          print traceback.format_exc().decode('utf-8')



    >>> Traceback (most recent call last):

      File "/var/folders/PC/PCtFE4gQGiqpQymiAScfnk+++TM/-Tmp-/py46506ECT", line 7, in <module>

        print traceback.format_exc().decode('utf-8')

    UnicodeEncodeError: 'ascii' codec can't encode characters in position 148-156: ordinal not in range(128) 






    On Thu, Jul 17, 2008 at 8:13 AM, Mark Tolonen <metolone+gmane at gmail.com> wrote:

      The Exception is output in the encoding of the source file.  If the terminal you are displaying the exception on is in a different encoding, it will be garbled.  I'm not familiar with OS X's terminal.  Try running python and printing sys.stdout.encoding.

      Alternatively, wrap your code in a try/except handler and translate the exception yourself.

          # coding: utf-8
          import traceback
          try:
              raise Exception(u'Зарегистрироваться')
          except Exception,e:
              print traceback.format_exc().decode('utf-8')

      The last line translates the utf-8 traceback into Unicode.  Printing Unicode will encode the output with the terminal's decoding.  If there are characters it can't display, you'll still get an error, though.  You can be more explicit however:

          print traceback.format_exc().decode('utf-8').encode('cp437','replace')

      In this case you'll get ? whenever a character can't be represented in the selected encoding.  cp437, for example, can't display any russian characters, so for me (on Windows) I just get all ???????????.  When I tried it with a character string that could be displayed in cp437, it worked fine:

          Traceback (most recent call last):

            File "<stdin>", line 1, in <module>
            File "t4.py", line 4, in <module>
              raise Exception('MàΓ£ΦΘΩδ')
          Exception: MàΓ£ΦΘΩδ

      Another option is to redirect the output to a file and read the file with an editor that can display utf-8 (such as Notepad on Windows).

          python testfile.py 2>error.txt          # this redirects stderr to a file.

      Hope that helps,
      Mark
        "Oleg Oltar" <oltarasenko at gmail.com> wrote in message news:b4fc2ad80807162050s1de6b6aalc86203c7e1fe3df5 at mail.gmail.com...
          The code
         # -*- coding: utf-8 -*-
        #!/usr/bin/python


        """

        This test case check how system works in the situation, when user tries to use already
        used username (domain)

        We are creating two accounts with such parameters:
        1. Sex = Femle
        2. Name1=Name2 = foobar%S 
        3. Pass1 = Name
        4. Pass2 = Name
        5. Email address1 = Email address2 =  Name at meta.ua 


        In the test we use verification point - warning message about incorrect input of domain name and the
        sugestion message

        """

        from selenium import selenium
        import unittest, time, re
        import HTMLTestRunner
        import config
        import Creating_account_basic




        class Same_domain_name(unittest.TestCase):
            
            def setUp(self):        
                self.name = "foobar"
                self.email = self.name + "@meta.ua" 
                self.verificationErrors = []
                self.selenium = selenium("localhost", 4444,config.browser, config.link)
                self.selenium.start()

            def test_create_account_to_check(self):  
                """Creating sample account for next test"""
                sel = self.selenium
                sel.open("/")
                sel.click(u"link=Регистрация")
                sel.wait_for_page_to_load("70000")
                sel.click("id_gender_1")
                sel.type("id_first_name", self.name)
                sel.type("id_last_name", self.name)
                sel.type("id_email", self.email)
                sel.type("id_username",  self.name)
                #sel.wait_for_condition(sel.is_element_present("check_username_block"), 70000)
                time.sleep(10)
                print "!!!", sel.is_element_present("check_username_block")
                sel.type("id_password",  self.name)
                print sel.get_text("check_username_block").decode('cp-1252')
                sel.type("id_password2", self.name)
                sel.click(u"//input[@value='Зарегистрироваться']")
                sel.wait_for_page_to_load("70000")
                if config.debugMode is True:
                    time.sleep(5)


            def tearDown(self):
                self.selenium.stop()
                print self.verificationErrors
                self.assertEqual([], self.verificationErrors)

        if __name__ == "__main__":
            
            unittest.main()
            #HTMLTestRunner.main()
         




        On Thu, Jul 17, 2008 at 6:47 AM, Oleg Oltar <oltarasenko at gmail.com> wrote:

          In [1]: import sys

          In [2]: sys.getdefaultencoding()
          Out[2]: 'ascii'

          In [3]: sys.stdout.encoding
          Out[3]: 'US-ASCII' 



          On Thu, Jul 17, 2008 at 6:29 AM, Oleg Oltar <oltarasenko at gmail.com> wrote:

            Seems need help there. Start getting 


            Traceback (most recent call last):

              File "./newaccount/Same_domain_name.py", line 56, in test_create_account_to_check
                print sel.get_text("check_username_block")
            UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)


            when trying to get the text of one of the elements. 

            How to solve it? 



            On Thu, Jul 17, 2008 at 5:11 AM, Oleg Oltar <oltarasenko at gmail.com> wrote:

              OK,

              I just run the program from terminal. OS: OS X, IDLE = Emacs:).

              Yep used the string "# -*- coding: utf-8 -*-" to setup encoding.... 



              On Thu, Jul 17, 2008 at 4:14 AM, Kent Johnson <kent37 at tds.net> wrote:

                Another possibility - do you have a coding declaration in your source
                file, something like
                # -*- coding: <encoding name> -*-

                If so, does the coding declaration match the actual encoding of the file?

                Kent


                On Wed, Jul 16, 2008 at 5:11 PM, Kent Johnson <kent37 at tds.net> wrote:
                > On Wed, Jul 16, 2008 at 2:40 PM, Oleg Oltar <oltarasenko at gmail.com> wrote:
                >> Hi I am using unittest framework with selenium.
                >>
                >> When I tried this code (my verification point)
                >>
                >>         self.assertEqual(True, sel.is_text_present(u"Извените пароли не
                >> совпадают"), "System didn't give a correct warning about the password
                >> misstype")
                >>
                >>> Where u"Извените пароли не совпадают" is russian = "Sorry passwords aren't
                >>> equal", and sel.is_text_present - searches text string on the page
                >>
                >> The output I get in case of failure was:
                >>
                >>
                >> Traceback (most recent call last):
                >>
                >>   File "./newaccount/Password_matching.py", line 50, in
                >> test_passwordMatching
                >>     self.assertEqual(True, sel.is_text_present(u"Извените
                >> пароли не Ñ Ð¾Ð²Ð¿Ð°Ð´Ð°ÑŽÑ‚"), "System didn't give a correct
                >> warning about the password misstype")
                >>
                >> AssertionError: System didn't give a correct warning about the password
                >> misstype
                >>
                >> Is there any way to get normal russian text instead of these strange D chars
                >> "Изве...."
                >
                > I don't have the solution but maybe I can give you a useful clue. The
                > D characters are most likely the utf-8 encoding of the Russian text,
                > when displayed as if it is latin-1. So something in the system is
                > converting the text to utf-8 and your console probably has latin-1 or
                > cp1252 encoding.
                >
                > Some details might help - how are you running the program - console,
                > IDLE...? What OS? What are the values of sys.getdefaultencoding() and
                > sys.stdout.encoding?
                >
                > Kent
                >











------------------------------------------------------------------------


        _______________________________________________
        Tutor maillist  -  Tutor at python.org
        http://mail.python.org/mailman/listinfo/tutor


      _______________________________________________
      Tutor maillist  -  Tutor at python.org
      http://mail.python.org/mailman/listinfo/tutor








------------------------------------------------------------------------------


  _______________________________________________
  Tutor maillist  -  Tutor at python.org
  http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080716/a5c9fecb/attachment-0001.htm>


More information about the Tutor mailing list