带有python测试的Vue Cypress代码覆盖率报告

2024-05-14 17:32:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Selenium进行Python测试,以测试我的Vue网页。我希望得到这些测试的代码覆盖率结果。我已经安装了babel插件来为应用程序添加工具,它可以正常工作。我有window.__coverage__对象,它包含覆盖率信息

我的问题是,如果我使用Python而不是Cypress的测试框架运行测试,如何获得覆盖率报告。我已经安装了cypress/代码覆盖率,但我不知道如何在不使用cypress的单元测试框架的情况下生成报告

另外,如果有另一个更适合我的用例的Vue覆盖率报告框架,我愿意接受建议


Tags: 工具对象代码插件框架应用程序网页报告
1条回答
网友
1楼 · 发布于 2024-05-14 17:32:04

以下是我在没有任何Cypress库的情况下如何做到这一点:

在测试文件的tearDownClass方法中(在所有测试之后),我添加了以下代码,以将window.__coverage__保存到.nyc_output/out.json文件中:

@classmethod
def tearDownClass(cls):
    # Put coverage data into file.
    data = cls.driver.execute_script('return window.__coverage__')
    
    with open(os.path.join(cls.webAppPath, ".nyc_output", "out.json"), 'w') as outfile:
        json.dump(data, outfile)

    # Generate HTML coverage report
    os.system("cd %s && %s report  reporter=html -e .vue" % (cls.webAppPath, os.path.join(cls.webAppPath, "node_modules", ".bin", "nyc")))

请注意.nyc_输出文件夹必须位于web应用程序的文件夹中。此外,在测试时,不应重新加载网页,因为这将重置覆盖率计数器,从而提供不正确的覆盖率

但是,这还不够。伊斯坦布尔的代码中有一个bug,所以我使用了一个补丁,findhere。在测试之前,我以这种方式修改了伊斯坦布尔的部分代码(然后重新编译web应用程序的代码):

# Fix a bug in istanbul code coverage lib
pathOfFileToModify = os.path.join(webAppPath, "node_modules/istanbul-lib-source-maps/lib/pathutils.js")

with open(pathOfFileToModify, "r") as fileToModify:
    modified = fileToModify.read().replace('relativeTo(file, origFile) {\n        return', 'relativeTo(file, origFile) {\n        if (origFile.indexOf(file) !== -1) { return origFile }\n        return')

with open(pathOfFileToModify, "w") as fileToModify: # TODO Maybe figure out how to use read-write mode?
    fileToModify.write(modified)

如果有人找到更好的方法,请在评论中告诉我

相关问题 更多 >

    热门问题