简体   繁体   English

如何适当地重用 mocha 测试的描述块?

[英]How to appropriately reuse the describe blocks of mocha tests?

I have an app that runs in different modes (think of it as running for different platforms as well as using different protocols), one of which has a long loading period every time a page is opened.我有一个以不同模式运行的应用程序(将其视为针对不同平台运行并使用不同协议),其中一个应用程序在每次打开页面时都有很长的加载时间。 There are some other minor changes, but all of those could just be taken care of using wdio's setting variables.还有一些其他小的变化,但所有这些都可以使用 wdio 的设置变量来处理。

Currently I have one test file (with a describe) for each section of the app.目前,我为应用程序的每个部分都有一个测试文件(带有描述)。 Which would be fine if one of the configurations being tested didn't have such a long wait time.如果正在测试的配置之一没有这么长的等待时间,那会很好。 Anyway, I've decided to deal with this test case, to handle it all in one file, which will all be on the same page.无论如何,我决定处理这个测试用例,将它全部处理在一个文件中,这些文件都将在同一页面上。

Anyway, instead of copying and pasting all the tests I had previously to this one large file I was wondering if I could somehow reuse them, as if they were functions.无论如何,与其将我之前的所有测试复制并粘贴到这个大文件中,我想知道是否可以以某种方式重用它们,就好像它们是函数一样。

As it is right now I did just wrap things in functions, so for example:就像现在一样,我只是将东西包装在函数中,例如:

// test1.js
module.exports = function test1 () {
  describe('Test1', function () {
    var settings = {}

    before(function () {
     // do something
    })

    it('do something', function () {
      assert.ok(true)
    })
    it('do something else', function () {
          assert.ok(true)
    })
  })
}

In another file we run every single function we created:在另一个文件中,我们运行我们创建的每个函数:

test1 = require('./test1')
test2 = require('./test2')
...
test10 = require('./test10')
describe('Main Test', function () {
  test1()
  test2()
  ...
  test10()
}

This would have solved my DRY problem, if I could somehow select which test functions to run upon my command using如果我能以某种方式选择在我的命令上运行哪些测试函数,这将解决我的 DRY 问题

wdio wdio/wdio.conf.js --specs wdio/test/spects/browser/test1.js wdio wdio/wdio.conf.js --specs wdio/test/spects/browser/test1.js

Which obviously will not work.这显然行不通。

Basically I want a solution to be able to reuse my tests (the describe blocks).基本上我想要一个能够重用我的测试(描述块)的解决方案。 Is what I was doing the right path?我所做的是正确的道路吗? If not, how should it be done?如果不是,应该怎么做?

So I have figured out the best way to go about this after I found some documentation about it here .所以在我在这里找到了一些关于它的文档后,我找到了解决这个问题的最佳方法。

I will just do as I previously described, however instead of shoving all those functions in the same file, I'll keep them in their own files.我会像我之前描述的那样做,但是我不会将所有这些函数都放在同一个文件中,而是将它们保存在自己的文件中。 There still may be a better solution out there, but it is still an improvement from copying and pasting all test cases for the different modes of running my app.仍然可能有更好的解决方案,但对于运行我的应用程序的不同模式,复制和粘贴所有测试用例仍然是一种改进。

Just programmatically create different describe blocks.只需以编程方式创建不同的描述块。 Wrap the describe block in a function with all the parameters that change (including the name of the block) and simply invoke the function to create the variations.用所有更改的参数(包括块的名称)将 describe 块包装在一个函数中,然后只需调用该函数即可创建变体。

I made a small repo to show this in practice: https://github.com/fatso83/forum-support-code/commit/cb2bc10b1d8bdae31e8f0a8c4e724c70583a5e11我做了一个小仓库来在实践中展示这一点: https : //github.com/fatso83/forum-support-code/commit/cb2bc10b1d8bdae31e8f0a8c4e724c70583a5e11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM