Python 作为unittests的一部分传递参数以测试pyspark脚本

Python 作为unittests的一部分传递参数以测试pyspark脚本,python,unit-testing,pyspark,python-unittest,args,Python,Unit Testing,Pyspark,Python Unittest,Args,我有一个python脚本,它当前使用命令行参数“path to the json file”,并对数据执行一些清理 我正在编写一些单元测试,试图将json文件的路径作为参数传递。当未传递参数时,它当前会出现错误,但当传递参数时,我会收到错误: AttributeError: 'module' object has no attribute 'data' which is data.json. 我希望有三个独立的单元测试,每个测试都有一个不同的json文件作为参数传递 我的代码如下: impo

我有一个python脚本,它当前使用命令行参数“path to the json file”,并对数据执行一些清理

我正在编写一些单元测试,试图将json文件的路径作为参数传递。当未传递参数时,它当前会出现错误,但当传递参数时,我会收到错误:

AttributeError: 'module' object has no attribute 'data' which is data.json. 
我希望有三个独立的单元测试,每个测试都有一个不同的json文件作为参数传递

我的代码如下:

import unittest
import sys
import argparse

class TestTransform(unittest.TestCase):
    def test_transform(self,input_filename):
        target = __import__("cleaning.py")
        transform = target
        transform.ARGS(input_filename)
        self.assertTrue('Pass')

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

如果我正确理解了你的问题,我通常会这样做。我覆盖
setUpClass
方法,并将所有可由测试访问的此类属性输入:

class TestTransform():

    @classmethod
    def setUpClass(self, file_name):
        self.input_filename = file_name
        #Some other initialization code here

    def test_transform(self):
        target = __import__("cleaning.py")
        transform = target
        transform.ARGS(self.input_filename)
        self.assertTrue('Pass')
如果您想使用不同的输入值进行不同的测试,您可以通过子类化
TestTransform
类来创建其他类(当然还有
unittest.TestCase
):


我得到一个错误,它是usage:test.py[-h]-f FILENAME test.py:error:argument-f/--FILENAME是必需的。需要在toti08时输入-f
class Test1(TestTransform, unittest.TestCase):
    @classmethod
    def setUpClass(self):
        input_filename = 'MyFileName'
        #Here call the setUpClass from the TestTransform class
        TestTransform.setUpClass(input_filename)