python - 后台进程的 cy.exec 超时

标签 python testing cypress

我正在尝试使用cy.exec启动服务器并将进程置于后台,如下所示:

cy.exec('nohup python -m my_module arg_1 &', { failOnNonZeroExit: false }).then(result => {
    if (result.code != 0) {
      throw new Error(`Execution of "${command}" failed
      Exit code: ${result.code}
      Stdout:\n${result.stdout}
      Stderr:\n${result.stderr}`);
    }
  })

但是,这会导致超时错误,就像进程在前台运行一样。我意识到使用 cy.exec 启动服务器是一种反模式,但我有一个非常特殊的情况,我需要为每个测试启动具有不同参数的服务器。 我认为通过后台进程我可以规避 cy.exec 的超时要求。

如果这不是一个选项,那么这种情况的最佳实践是什么,即每个 cypress 测试启动不同的服务器?

最佳答案

您可以尝试使用 cypress 任务而不是“cy.exce”

示例 在你的 cypress/plugins/index.js 中

const startServer = async function (ExecuteCommandWithPath) {
    
    exec(ExecuteCommandWithPath,
        (error, stdout, stderr) => {
            console.log(stdout);
            console.log(stderr);
            if (error !== null) {
                console.log(`exec error: ${error}`);
                return false;
            }
            return true;
        });
};

module.exports = ( on, config ) => {
  on("task", {
PythonServerStatUp(ExecuteCommandWithPath) {

            return new Promise((resolve, reject) => {
                startServer(ExecuteCommandWithPath);
                resolve(false);
            });
        },
});
}

关于python - 后台进程的 cy.exec 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71727463/

相关文章:

python - 在 python 中覆盖 {}

python - 这种递归二分搜索算法可以更有效吗?

go - 如何在保留测试可比性的同时使用动态误差?

angular - 为什么在使用 Cypress 时需要显式清除 sessionStorage?

javascript - 无法使用 Cypress.io 测试页脚的背景颜色,它会抛出错误

python - 在循环中恢复循环的Pythonic方法是什么

python - 从 Python 中的字符串中删除“”的正则表达式

ruby-on-rails - RSpec 无法在共享示例中使用定义的变量

ruby-on-rails - 将同名参数传递给 Rspec POST 请求以创建数组

cypress - 在 cypress 中输入变量,然后按 Enter 键