我有一个包含多个测试套件的项目。我希望能够指定要在命令行中运行的套件:
suite=multiplication python3 .
这是我当前的文件结构:
__main__.py
suites/
__init__.py
addition.py
subtraction.py
multiplication.py
division.py
suites / __ init __。py
__all__ = ['addition', 'subtraction', 'multiplication', 'division']
subtraction.py
def testSuite():
# Bunch of tests
__ main __。py
import os
import suites
# Get suite name from 'suite=xxx' in command line
suiteName = os.getenv('suite')
# Based on suiteName, load the correct file
suite = suites[suiteName]
# Call the suite loaded from the file
suite()
出现此错误:
suite = suites[suiteName]
TypeError: 'module' object is not subscriptable
有条件地从另一个软件包导入和运行脚本的最佳方法是什么?
答案 0 :(得分:4)
from importlib import import_module
suite = import_module('suites.' + suiteName)
suite.testSuite()