python - Golang单元测试python函数

标签 python unit-testing go

我在 Golang 中有一个调用 python 函数的 API 处理程序。我如何模拟来自 python 函数的响应以避免依赖该函数正确运行来测试 Golang 函数?

最佳答案

您可以将您的函数包装到一个新的 moc 函数中:

func CallPythonFunctionMoc() Result {
    var res Result
    var err error
    res, err = CallPythonFunction()
    if err != nil {
        res = "Moc value"
    }
    return res

编辑:

如果您实际上不想调用 python 函数,只需返回 moc 值:

func CallPythonFunctionMoc() Result {
    res := "Moc value"
    return res

关于python - Golang单元测试python函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44915711/

相关文章:

python - cwd的字符串变量

unit-testing - 我如何将 should 与 mocha 和 mongoose 一起使用?

python - 从列表中的字典元组创建字典的字典

python - 如何对元组中的一组列表中的第一个值求和?

python - 如何计算 GradientBoostingClassifier 的特征重要性

ruby - 相当于Ruby 1.9.2中nUnit的TestCase Test::Unit

java - 使用 mockito 模拟带有 Object 参数的方法

go - 这个函数在哪里返回非错误?

go - io.Pipe 中的竞争条件?

pointers - 满足接口(interface)的 Go 结构方法类型