将 args 作为单元测试的一部分传递以测试 pyspark 脚本
Passing args as part of unittests to test pyspark script
我有一个 python 脚本,它目前接受一个命令行参数 'path to the json file' 并对数据进行一些清理。
我正在编写一些单元测试,我试图将路径作为参数传递给 json 文件。当没有传递 arg 时它当前出现错误但是当它传递时我得到错误:
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 属性输入了所有可以被测试访问的属性:
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')
如果您随后想要使用不同的输入值进行不同的测试,您可以通过子classing TestTransform
class 创建其他 classes(当然unittest.TestCase
):
class Test1(TestTransform, unittest.TestCase):
@classmethod
def setUpClass(self):
input_filename = 'MyFileName'
#Here call the setUpClass from the TestTransform class
TestTransform.setUpClass(input_filename)
我有一个 python 脚本,它目前接受一个命令行参数 'path to the json file' 并对数据进行一些清理。
我正在编写一些单元测试,我试图将路径作为参数传递给 json 文件。当没有传递 arg 时它当前出现错误但是当它传递时我得到错误:
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 属性输入了所有可以被测试访问的属性:
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')
如果您随后想要使用不同的输入值进行不同的测试,您可以通过子classing TestTransform
class 创建其他 classes(当然unittest.TestCase
):
class Test1(TestTransform, unittest.TestCase):
@classmethod
def setUpClass(self):
input_filename = 'MyFileName'
#Here call the setUpClass from the TestTransform class
TestTransform.setUpClass(input_filename)