I have a method which does the following. Question is how do I unit test this method. I am pretty new to this Python unit testing module.
The question and solution are as follows:
Given a string containing of ‘0’, ‘1’ and ‘?’ wildcard characters, generate all binary strings that can be formed by replacing each wildcard character by ‘0’ or ‘1’.
Example :
Input str = "1??0?101"
Output:
10000101
10001101
10100101
10101101
11000101
11001101
11100101
11101101
Solution:
def _print(string, index):
if index == len(string):
print(''.join(string))
return
if string[index] == "?":
# replace '?' by '0' and recurse
string[index] = '0'
_print(string, index + 1)
# replace '?' by '1' and recurse
string[index] = '1'
_print(string, index + 1)
# NOTE: Need to backtrack as string
# is passed by reference to the
# function
string[index] = '?'
else:
_print(string, index + 1)
# Driver code
if __name__ == "__main__":
string = "1??0?101"
string = list(string) #don’t forget to convert to string
_print(string, 0)
Output:
10000101
10001101
10100101
10101101
11000101
11001101
11100101
11101101
Questions:
1. Also, is there a way of returning a list as output instead of printing them out?
2. Which assert test cases are appropriate in this scenario?
3. What would be the best end to end test cases to cover in this case?
4. What could be a better approach of solving this in terms of time and space complexity?
I have tried this which doesn't seem to work:
import unittest
from wildcard import _print
class TestWildCard(unittest.TestCase):
def test_0_print(self):
print("Start wildCard _print test: \n")
result = 111
self.assertEquals(_print("1?1",0),result,"Results match")