Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automated Testing Reverse String in Python 3

I am trying to create a function that can test two different functions.

I am naming my test function test_reverse, to automate testing of strReverseI and strReverseR (see the code for these below)

strReverseI and strReverseR will be arguments to test_reverse.

test_reverse will have one parameter, f, a function (either strReverseR or strReverseI)

test_reverse will call f repeatedly, for a series of test cases, and compare the result returned by either strReverseR or strReverseI to the expected result. test_reverse should report for each test case whether the actual result matches the expected result, e.g.,

Checking strReverseR('testing123')...its value '321gnitset' is correct!
Checking strReverseI('a')... Error: has wrong value 'a', expected 'b'. 

Function test_reverse should return None.

Test cases and expected results should be stored in a tuple of tuples defined in test_reverse. test_reverse should include at least the following tests:

(
('', ''),
('a', 'a'),
('aaaa', 'aaaa'),
('abc', 'cba'),
('hello', 'olleh'), ('racecar', 'racecar'), 
('testing123', '321gnitset'), ('#CIS 210', '012 SIC#'), ('a', 'b') 
)

Here is the code I have so far… strReverseR and strReverseI work perfectly, I am not just needing to figure out test_reverse:

import doctest

def strReverseI(s):
'''
(str) -> (str)
Returns the reverse of the string input. This function finds 
the reverse string via an interative implementation.


NOTE TO MYSELF: I could also have chosen to do a while loop being

(s[n:] != ''):

This is using a while loop and as long as the slice of the string 
is not an empty string,
it will continue to run the loop. As soon as the slice becomes an 
empty string,
it will stop the loop and you will have the reverse string.

Examples:

>>> strReverseI('hello, world')
'dlrow ,olleh'
>>> strReverseR('')
''
>>> strReverseR('a')
'a'
'''
    result = ""
    n = 0
    for i in range(1, len(s) + 1):
        result += s[len(s) - i]
    return result

def strReverseR(s):
'''
(str) -> (str)
Returns the reverse of the string input. This function finds 
the reverse string via recuersion. The base case would be an empty 
string so the function would still return out an empty string and 
not have any errors.
After we can confirm the string is not empty,
 we return the last element of the string, followed by
the string without that last element/character. Since this 
continues to call upon itself, it will continue to cut the string
of the "last" element to grow the reverse string. 

Examples:

>>> strReverseR('hello, world')
'dlrow ,olleh'
>>> strReverseR('')
''
>>> strReverseR('a')
'a'
''' 
    if s == '':
        return s
    result = s[-1] + strReverseR(s[0:-1])
    return result

def test_reverse(f):
'''
function ->

'''
    if f('hi') == 'ih':
        print("success")
    else:
        print("failed")
    return None

Note Yes, this is something I am working on for a school hw assignment. I am not expecting anyone to write it for me. I am just really confused on this last part and would love some insight on how to move forward. Any examples, and explanations would be so helpful. Thanks.

Update I have been working on this more and this is along the lines of what I think I need to do, if you could help a bit with this that would be great!

def test_reverse(f):
'''
function ->

'''

list = (
        ('', ''),
        ('a', 'a'),
        ('aaaa', 'aaaa'),
        ('abc', 'cba'),
        ('hello', 'olleh'), ('racecar', 'racecar'),
        ('testing123', '321gnitset'), ('#CIS 210', '012 SIC#'), ('a', 'b')
        )

for x in list:

    #f(i[0]) = [1])

    if f(i[0] == [1]):

        print("Checking", f,"... its value", "is correct!")

    else:

        print("Checking", f, "... Error: has wrong value")

return None

def main(): '''calls string reverse test func 2 times''' test_reverse(p5.strReverseR) print() test_reverse(p5.strReverseI) return None

like image 604
Alyssa Kelley Avatar asked Aug 25 '26 23:08

Alyssa Kelley


2 Answers

You should consider using a tool like unittest for this - it has assert style tests which make calling methods and seeing their output easy.

For your use case, you probably want to create tests.py, something like:

import unittest
from myreversemodule import strReverseL, strReverseR


class ReverseTestCase(unittest.TestCase):

    def test_strreverser(self):
        self.assertEqual(strReverseR('hi'), 'ih')

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

Now run this script - and it will give you nice test output, show errors, return True or False if it succeeds/fails. You can also run specific tests by name if you only want to test a subset, and use setUp/tearDown methods to do some config before/after running each test/each suite of tests.

like image 80
match Avatar answered Aug 29 '26 10:08

match


Without inherited of unittest you can use builtins AssertionError:

def test_case(first_string, reversed_string):
    if strReverseR(first_string) != reversed_string:
        raise AssertionError("Reverse of first string: " + strReverseR(first_string) + " are not equals to second string: " + reversed_string)

Hope, it helps.

like image 22
V. Rob Avatar answered Aug 29 '26 12:08

V. Rob



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!