简体   繁体   中英

Python List Unittest

I have a situation where I'm trying to test a function with the unittest module that returns a list . I've been able to figure out how to setup a basic unittest with assertEqual ; however, I'm thinking I need something different to test a list. Maybe asserEqualList ?

As shown in the code below, I'm testing the md5_finder function that takes in two text files. The list that is returned from this function should contain a single item that equals 20211027-eclipsebio/03918A_207g1_S25_R1_001.fastq.gz .

When trying to run this code, I'm getting:

AssertionError: ['20211027-eclipse/03918A_207g1_S25_R1_001.fastq.gz'] != '20211027-eclipse/03918A_207g1_S25_R1_001.fastq.gz'

import sys
sys.path.insert(0, '../../../src')
import s3_organizer
import unittest
import argparse


class TestCalc(unittest.TestCase):

    def test_match(self, md5_seqbucket, md5_other):

        # create list to test

        # list_test = ['20211027-eclipse/03918A_207g1_S25_R1_001.fastq.gz']

        self.assertEqual(s3_organizer.md5_finder(md5_seqbucket, md5_other),
                         '20211027-eclipse/03918A_207g1_S25_R1_001.fastq.gz')



def main(md5_seqbucket, md5_other):

    TestCalc.test_match(TestCalc(), md5_seqbucket, md5_other)

    unittest.main()


if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='test individual functions inside s3_organizer')

    parser.add_argument("--md5_seqbucket",
                        help='md5 of FASTQs in seqbucket')

    parser.add_argument("--md5_other",
                        help='md5 of FASTQs in other bucket')

    # parse out arguments

    args = parser.parse_args()

    sys.exit(main(args.md5_seqbucket, args.md5_other))

It's obvious to me that the string I'm passing in is not a list; however, when I do try to pass in something like list_test = ['20211027-eclipse/03918A_207g1_S25_R1_001.fastq.gz'] the entire function breaks and doesn't give me a useful error:

usage: test_s3_organizer.py [-h] [-v] [-q] [--locals] [-f] [-c] [-b] [-k TESTNAMEPATTERNS] [tests [tests ...]]
test_s3_organizer.py: error: unrecognized arguments: --md5_seqbucket --md5_other test_case2.txt

The same thing happens when trying to use asserEqualList .

Compare the result to a list containing the desired value.

    result = s3_organizer.md5_finder(md5_seqbucket, md5_other)
    self.assertListEqual(result, ['20211027-eclipsebio/03918A_207g1_S25_R1_001.fastq.gz'])

Or check that the result contains only one element and that said element is the one you want.

    self.assertEqual(len(result), 1)
    self.assertEqual(result[0], '20211027-eclipsebio/03918A_207g1_S25_R1_001.fastq.gz')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM