Search code examples
pythonassertions

Assertion not valid in script though type comparison works in terminal


I encounter a quite strange issue with comparison.

I want to decode python script arguments, store them and analyse them. In the following command, the -r option shall be used to determine which type of report to create.

python script launching : %run decodage_parametres_script.py -r junit,html

python script options parsing is used to fill a dictionary, the result is: current_options :

{'-cli-no-summary': False, '-cli-silent': False, '-r': ['junit', 'html']}

Then I want to test the -r options, here is the code:

for i in current_options['-r']:
    # for each reporter required with the -r option:
    #    - check that a file path has been configured (otherwise set default)
    #    - create the file and initialize fields
    print("trace i", i)
    print("trace current_options['-r'] = ", current_options['-r'])
    print("trace current_options['-r'][0] = ", current_options['-r'][0])

    if current_options['-r'][i] == 'junit':
        # request for a xml report file
        print("request xml export")
        try:
            xml_file_path = current_option['--reporter-junit-export']
            print("xml file path = ", xml_file_path)
        except:
            # missing file configuration
            print("xml option - missing file path information")
            timestamp = get_timestamp()
            xml_file_path = 'default_report' + '_' + timestamp + '.xml'
            print("xml file path = ", xml_file_path)
        if xml_file_path is not None:
            touch(xml_file_path)
            print("xml file path = ", xml_file_path)
        else:
            print('ERROR: Empty --reporter-junit-export path')
            sys.exit(0)
    else:
        print("no xml file required")    

I want to try the default report generation but I don't even hit the print("request xml export") line, here is the console result :

trace i junit
trace current_options['-r'] =  ['junit', 'html']
trace current_options['-r'][0] =  junit

As I guess it could be a type issue, I tried the following tests:

In [557]: for i in current_options['-r']:
 ...:     print(i, type(i))
 ...:
junit <class 'str'>
html <class 'str'>

In [558]: toto = 'junit'

In [559]: type(toto)
Out[559]: str

In [560]: toto
Out[560]: 'junit'

In [561]: toto == current_options['-r'][0]
Out[561]: True

so my line assertion if current_options['-r'][i] == 'junit': should end up begin True but it's not the case. Am I missing something trivial ??? :(

Can someone help me, please ?


Solution

  • You are iterating by array of strings

    for i in current_options['-r']:
    

    in your case i will be:
    junit on first iteration
    html on next iteration

    and your if condition (from interpreter perspective) will looks like:

      if current_options['-r']['junit'] == 'junit':
    

    instead of expected:

      if current_options['-r'][0] == 'junit':
    

    Solution 1:
    You need iterate through of range(len(current_options['-r']))

    Solution 2
    change your comparator:
    from

    if current_options['-r'][i] == 'junit':
    

    to

    if i == 'junit':