首页 > 解决方案 > 如何停止在 K6 中复制脚本?

问题描述

我必须在 K6 中为一个应用程序编写大约 20 个不同的脚本。而且这些脚本中的大多数都包含常见的功能,如登录、选择一些选项等......

那么有没有更好的方法来编写 K6 脚本而不复制这些常用功能呢?我们可以在某处实现通用方法并在默认函数或类似的函数中执行它吗?

标签: load-testingk6

解决方案


您可以编写自己的模块包含常用功能,然后导入它们:

$ cat index.js
import { hello_world } from './modules/module.js';

export default function() {
    hello_world();
}
$ cat module.js
export function hello_world() {
    console.log("Hello world");
}

你可以在这里阅读更多细节。


推荐阅读