为管道中的函数编写单元测试

时间:2021-07-18 04:45:17

标签: python unit-testing

我有按顺序处理的数据,从

状态 A -> 状态 B -> 状态 C

如果我有函数

def convertAtoB(obj):...

def convertBtoC(obj):...

我将如何编写单元测试来测试 convertBtoC?由于单元测试必须相互独立,所以我不能在单元测试中调用 convertAtoB 来创建状态 B 的对象。

1 个答案:

答案 0 :(得分:1)

要么创建 B 的模拟版本,要么创建 B 的完整静态版本并将其传递给 convertBtoC

例如:

fetchJSONWeatherOverHTTP(weatherRequest) -> convertJSONWeatherIntoDataclass(jsonWeather)

weatherRequest -> jsonWeather -> weather

在这种情况下,我们需要使用假数据模拟 jsonWeather 对象或使用旧结果,因为我们不想在单元测试中执行 http 请求。