我有一个luigi任务,它需要的输入是“test_file.txt”。我想通过测试类来更改所需的输入。我需要更改输入,以便可以使用文件测试类的功能。我尝试了下面的代码,但打印的结果仍然是初始路径--“test_file.txt”。如何仅在测试中更改路径?(以获取“data.json”作为打印结果
import pytest
import luigi
class LuigiToBeTested(luigi.ExternalTask):
def requires(self):
return luigi.LocalTarget("test_file.txt")
def test_Luigi():
class_instance = LuigiToBeTested()
class_instance.requires().path = 'data.json'
print('/////', class_instance.requires().path) #to get data.json
这里有3个解决方案
test_luigi_1
-截取(通过函数)初始化luigi.LocalTarget
的新对象并替换输入文件名的所有调用test_luigi_2
-与1相同,但通过子类test_luigi_3
-不要像1和2中那样进行修补,而是通过允许dependency injection重新设计源代码以使其可测试。这是一种更具扩展性的方式,也更能证明未来aAsk for things, Don't look for things (aka Dependency Injection / Law of Demeter)
bFixing the Client API: Dependency Injection
c。还有更多关于为什么这是首选方式的参考资料
相关问题 更多 >
编程相关推荐