日常生活的摩擦龃龉,十之其九起于口舌。——贝涅特
推荐使用HBuilderX uni-app
自动化测试插件
https://ext.dcloud.net.cn/plugin?id=5708
安装完毕,右键项目选择uni-app
运行自动化测试即可
这里可以在HBuilderX
设置的插件设置里取消勾选“自动修改jest.config.js
文件中的testMatch
”选项才能自定义测试目录
这是我的jest.config.js
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| module.exports = { globalTeardown: '@dcloudio/uni-automator/dist/teardown.js', testEnvironment: '@dcloudio/uni-automator/dist/environment.js', testEnvironmentOptions: { compile: true, h5: { url: "http://localhost:8000/h5/", options: { headless: false } }, "app-plus": { android: { appid: "__UNI__88958F5", package: "com.roben.achao", executablePath: "HBuilderX/plugins/launcher/base/android_base.apk" }, ios: { id: "8277B3AD-794C-4D26-9A16-4CEC2F3FDE1A", executablePath: "HBuilderX/plugins/launcher/base/Pandora_simulator.app" } }, }, testTimeout: 15000, reporters: [ 'default' ], watchPathIgnorePatterns: ['/node_modules/', '/dist/', '/.git/'], moduleFileExtensions: ['js', 'json'], rootDir: __dirname, testMatch: ["<rootDir>/__tests__/**/*test.[jt]s?(x)"], testPathIgnorePatterns: ['/node_modules/'] }
|
由于无法直接调用,所以新建了一个test.nvue
页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| <template> <view> test </view> </template>
<script> export default { data() { return {} }, methods: { req(method, params) { return uni.$u.api[method](params) } } } </script>
<style>
</style>
|
然后编写实际的测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| describe('common/http.api.js', () => { let page beforeAll(async () => { page = await program.navigateTo('/pages/test/test') await page.waitFor(1000) })
it('common/http.api.js', async () => { const res = await page.callMethod('req', 'getPhoneCode', { "mobile": 'a12312x', "scene": 1 }) expect(res).toEqual({ code: 0, data: true, msg: '' }); }) })
|