Prefixing test case methods with letter 'test'

Hi,

I have a Python unit test cases source code file which contains more than a hundred test case methods. In that, some of the test case methods already have prefix 'test' where as some of them do not have. Now, I need to add the string 'test' (case-sensitive) as a prefix to those of the methods that have this characters absent. Also, I need to change the case of the first letter of the method following 'test'.

for eg:

def testGroupGetVersion(self): --> this Python test case method is already prefixed with the string 'test', so this has to be ignored

def setCurrentGroup(self, listID, listName, path, clientID): --> this method do not have that prefix 'test', so changed it to 

def testSetCurrentGroup(self, listID, listName, path, clientID):  Note: the case of the first letter of the method has been changed (from lowercase to uppercase) 

Please help me in accomplishing this task and thank you for your help.

not tested...

 
awk '/def/ && $2!~/test/{$2="test"$2}1' input.txt

Cool itkamaraj!! thanks for the instant reply :slight_smile: Any possibility using Perl/sed, so that I can do in-place editing of the file?

 
$ perl -lane 'if($F[1]!~/test/){$F[1]="test" . "\u$F[1]";}print join " ",@F' input.txt
def testGroupGetVersion(self):
def testSetCurrentGroup(self, listID, listName, path, clientID):

1 Like

Thank you for your help. However, the tab indentations in the file is getting removed by both of your awk and perl suggestions !! Any idea how to get rid of it?

perl -lane 'if($F[1]!~/test/){$F[1]="test" . "\u$F[1]";}print join "\t",@F' input.txt