如何解决运行测试时找不到功能的代码覆盖问题? [英] How to fix issue of code coverage not finding functions when run tests do?

查看:117
本文介绍了如何解决运行测试时找不到功能的代码覆盖问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行我的测试时,它们全部通过.当运行代码覆盖4时,由于功能未定义而失败.定义了它们(测试通过时清楚地显示了它们).我无法获得在那些测试中测试了这4个功能的文件的代码覆盖率,这不好,因为我需要知道这些功能是否经过了全面测试.

When running my tests they all pass. When running code coverage 4 fail due to functions being 'not defined'. They are defined (as the tests clearly show when they pass). I cannot get the code coverage for the file that has the 4 functions being tested in those tests which is not good as I need to know if the functions are fully tested.

我在PHPStorm上.用于这些测试的jsTestDriver有一个由我制作的文件,即Sinon v7.3.2,jQuery v1.11.1和D3.js v5.9.7作为依赖项.

I am on PHPStorm. The jsTestDriver for these tests has a file made by me, Sinon v7.3.2, jQuery v1.11.1, and D3.js v5.9.7 as dependencies.

load:
  - buttonActions.js
  - tests/dependencies/jquery.js
  - tests/dependencies/sinon732.js
  - tests/dependencies/d3.js

test:
  - tests/Functional/javascriptTests/actionsTestButtonActionsFunctions.test.js

timeout: 30

buttonActions.js中的部分代码:

Partial code from buttonActions.js:

function _hideOrShow(table){

    var classNames = table[0]["attributes"][0]['nodeValue'];
    var secondClass = classNames.replace("sortable table-striped table-responsive ",'');

if (secondClass === "veryHidden" ) {
    table.removeClass("veryHidden").addClass("veryShown");
} else {
    table.removeClass("veryShown").addClass("veryHidden");
}

}

我的测试中的部分代码:

Partial code from my tests:

 ActionsTestButton = TestCase("ActionsTestButtonActions");

 ActionsTestButton.prototype.setUp = function() {
//stuff done here. Not important
}

ActionsTestButton.prototype.testHideOrShow = function () {

var table = {
    class: "sortable table-striped table-responsive veryHidden",
    0: {
        attributes: {
            0: {
                nodeValue: "sortable table-striped table-responsive veryHidden"
            }
        }
    },
    removeClass: function (name){
        table.class = table.class.replace(name, '');
        table[0]["attributes"][0]['nodeValue'] = table.class;
        return {
            addClass: function (name){
                table.class = table.class+name;
                table[0]["attributes"][0]['nodeValue'] = table.class;
            }
        }
    }

 };

assertEquals("sortable table-striped table-responsive veryHidden", table.class);
assertEquals("sortable table-striped table-responsive veryHidden", table[0]["attributes"][0]['nodeValue']);

_hideOrShow(table);

assertEquals("sortable table-striped table-responsive veryShown", table.class);
assertEquals("sortable table-striped table-responsive veryShown", table[0]["attributes"][0]['nodeValue']);

_hideOrShow(table);

 assertEquals("sortable table-striped table-responsive veryHidden", table.class);
 assertEquals("sortable table-striped table-responsive veryHidden", table[0]["attributes"][0]['nodeValue']);

 };

     ActionsTestButton.prototype.tearDown = function() {
document.getElementById.restore();
window.$.restore();
window.d3.select.restore();

 };

错误消息我正在运行代码覆盖范围:

Error message I get running code coverage:

     line 178:4 mismatched input 'const' expecting RBRACE
     line 1022:12 no viable alternative at input 'throws'
     line 1236:13 mismatched input 'throws' expecting Identifier
     line 1236:31 extraneous input 'throws' expecting LPAREN
     line 3998:12 no viable alternative at input 'function'
     line 5123:14 missing Identifier at 'in'
     line 5123:17 no viable alternative at input '='
     line 5413:8 no viable alternative at input '}'
     line 5415:30 no viable alternative at input ']'

     ReferenceError: _removeIcons is not defined
at b.ActionsTestButton.testPrivateRemoveIcons (http://127.0.0.1:9876/test/tests/Functional/javascriptTests/actionsTestButtonActionsFunctions.test.js:179:5)
at TestResultIterator.runTest (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:201:28)
at TestResultIterator.runNext (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:292:10)
at InstrumentedTestCaseRunner.run (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:250:27)
at InstrumentedTestCaseRunnerPlugin.runTestConfiguration (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:221:20)



     ReferenceError: _reAddIcon is not defined
at b.ActionsTestButton.testPrivateReAddIcons (http://127.0.0.1:9876/test/tests/Functional/javascriptTests/actionsTestButtonActionsFunctions.test.js:196:5)
at TestResultIterator.runTest (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:201:28)
at TestResultIterator.runNext (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:292:10)
at InstrumentedTestCaseRunner.run (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:250:27)



     ReferenceError: _removeIcons is not defined
at b.ActionsTestButton.testRemoveThenAddRevertsBackToBlock (http://127.0.0.1:9876/test/tests/Functional/javascriptTests/actionsTestButtonActionsFunctions.test.js:214:5)
at TestResultIterator.runTest (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:201:28)
at TestResultIterator.runNext (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:292:10)
at InstrumentedTestCaseRunner.run (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:250:27)



     ReferenceError: _hideOrShow is not defined
at b.ActionsTestButton.testHideOrShow (http://127.0.0.1:9876/test/tests/Functional/javascriptTests/actionsTestButtonActionsFunctions.test.js:251:5)
at TestResultIterator.runTest (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:201:28)
at TestResultIterator.runNext (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:292:10)
at InstrumentedTestCaseRunner.run (http://127.0.0.1:9876/test/com/google/jstestdriver/coverage/javascript/LCOV.js:250:27)



     Process finished with exit code 0

我只想获取此文件的代码覆盖率.它适用于我所有其他文件测试.只是不是这个.

I just want to get code coverage for this file. It works for all my other file tests. Just not this one.

推荐答案

JSTestDriver中的代码覆盖范围不喜欢letconst.它不会在包含它的任何文件上运行代码覆盖范围,并且不允许包含它的文件中的功能.

Code coverage in JSTestDriver does not like let and const. It will not run code coverage on any files that contain it and will not allow functions from a file that contains it.

对于我的特殊情况,我有一个使用const的函数.我正在测试的函数中没有调用使用它的函数,因此从未进行过测试.这意味着测试通过了.但是,在同一个文件中足以使代码覆盖范围中断.

For my particular case I had a function that used const. The function that used it was not called in the functions I was testing and thus was never tested at all. This meant the tests passed. However, being in the same file was enough to make code coverage break.

我的解决方案?将letconst都更改为var.从语义上讲,这可能不是最好的主意,但就我而言,这对我的代码或行为没有明显的影响.

My solution? Change both let and const to var. Semantically it may not be the best idea but in my case it made no noticeable difference to my code or the behaviour.

这篇关于如何解决运行测试时找不到功能的代码覆盖问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆