Jay Joshi
Jay Joshi

Reputation: 1562

Testing the python code using non-ascii characters

We are working on migrating a Python 2 code to Python 2&3 compatible code.

We are using futurize for the migration process.

Well, as we all know, there are a lot of issues of Unicode and byte string across the Python 2&3 compatibility. For now, we have only tested the code with simple English characters (ASCII characters)

How can we unit/functional test the methods to check that it won't break when non-ASCII characters are provided as input?

def any_random_method(some_string):
    # Some code
    return some_return_string

How should I unit test this with non-ascii characters.? A code snippet would be a great help.

p.s I am very confused with how to provide the non-ascii characters specifically to even start the unit testing. and... I want to unit test it with Python 2 and Python 3 both. so the unit test should support both of the versions to handle the non-ascii characters while testing

Edit:

Upvotes: 1

Views: 1116

Answers (2)

amol goel
amol goel

Reputation: 149

Borrowing string from above solution: Take a non-ascii string : 'Όταν λείπει η γάτα, χορεύουν τα ποντίκια.'

  1. get output of function_abc in python-2 code: let's say "xyz"
  2. make unitTest explained as above.
  3. Run unitTest on python 3
  4. Check that output of function is same as in python-2 code

class TestNotBreakFunctions(unittest.TestCase):

def setUp(self):
    self.string_utf8 = 'Όταν λείπει η γάτα, χορεύουν τα ποντίκια.'

def test_abc(self):
    result= function_abc(self.string_utf8)
    self.assertEqual("xyz",result)

Upvotes: 0

kederrac
kederrac

Reputation: 17322

you can use:

# -*- coding: utf-8 -*-
import unittest


def func1(my_string):
    # your code
    pass


def func2(my_string):
    pass


class TestNotBreakFunctions(unittest.TestCase):

    def setUp(self):
        self.string_utf8 = 'Όταν λείπει η γάτα, χορεύουν τα ποντίκια.'

    def test_func1(self):
        try:
            func1(self.string_utf8)
        except Exception as e:
            self.assertTrue(False, e)

    def test_func2(self):
        try:
            func2(self.string_utf8)
        except Exception as e:
            self.assertTrue(False, e)


if __name__ == '__main__':
    unittest.main()

the example is base on the assumption that you have one argument of type str but you can use any kind of arguments, just use try except

Upvotes: 2

Related Questions