简体   繁体   中英

Running a Python unittest test on multiple files

I want to add unit testing to the assessment of my high school programming class.

If I have twenty submissions of files that look like this:

def calculateReturn(principle, rate, freq, time):
    final = principle * (1 + (rate/freq)) ** (freq * time)
    return final

Can I use a test case like this?

import unittest

class test(unittest.TestCase):
    def test1(self):
        value = calculateReturn(5000, 0.05, 12, 11)
        self.assertAlmostEqual(value, 8235.05, 2)

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

How do I run this one simple test on twenty modules?

FURTHER INFORMATION

For testing I have created three "submissions" all of which show different ways of calculating x^y.

submission1.py:

from math import pow

def powerFunction(base, power):
   result = pow(base, power)
   return result

submission2.py:

def powerFunction(base, power):
    result = base ** power
    return result

submission3.py:

def powerFunction(base, power):
    result = 1
    for i in range(power):
        result = result * base

    return result

The test code is:

import unittest
import importlib

class MyTest(unittest.TestCase):
    def setUp(self):
        pass

    def test_power_3_4(self):
        self.assertEqual(module.powerFunction(2, 3), 8)

files = ['submission1', 'submission2', 'submission3']
for file in files:
    module = importlib.import_module(file)
    print module
    unittest.main()

if the test code is run the console output shows only submission1 being tested:

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
/Users/staff/PycharmProjects/UnitTest/powerTest.py
<module 'submission1' from '/Users/staff/PycharmProjects/UnitTest/
submission1.pyc'>
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Process finished with exit code 0

Interestingly if I don't use unit testing I can correctly import and test using this approach:

import importlib

files = ['submission1', 'submission2', 'submission3']

for file in files:
    module = importlib.import_module(file)
    print module
    print module.powerFunction(2,3)

The console output here is:

/System/Library/Frameworks/Python.framework/Versions/2.7/bin/
python2.7 /Users/staff/PycharmProjects/UnitTest/importlib1.py
<module 'submission1' from '/Users/staff/PycharmProjects/UnitTest/
submission1.pyc'>
8.0
<module 'submission2' from    '/Users/staff/PycharmProjects/UnitTest/
submission2.pyc'>
8
<module 'submission3' from   '/Users/staff/PycharmProjects/UnitTest/
submission3.pyc'>
8

Process finished with exit code 0

It may well be that the unittest module is not the best approach here but I'm still interested on how to implement it.

You can use importlib to load Python modules from specific files, then run the test cases on each one.

glob may be helpful to create the list of files.

Given that this has been active for a month with no answers I have come the the realisation that it is because I'm asking for the wrong thing.

From what I can gather, unittest is for running a suite of tests on a single application. It is not designed to run a single test on a suite of applications.

John's suggestion to investigate importlib helped set me on the path to success. Thanks John.

The code posted in the original post update seems to be the most appropriate solution to my problem.

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