Skip to content

Commit

Permalink
Remove FS_COVER to keep the work simple.
Browse files Browse the repository at this point in the history
  • Loading branch information
supplient committed Jun 7, 2019
1 parent ca98763 commit b56948d
Show file tree
Hide file tree
Showing 8 changed files with 1,612 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Expand Up @@ -9,5 +9,5 @@ __pycache__/
test.log

# coverage
coverage/
JSCover-all.jar
coverage/back/*
coverage/front/*
Binary file added JSCover-all.jar
Binary file not shown.
2 changes: 0 additions & 2 deletions TODO
@@ -1,5 +1,3 @@
* 将所有subTest改成失败的话,直接整个TC都fail,因为我使用的所有的SubTest都只是表示的一个线性操作序列而已。
* 为前测试点置换TestRunner,使得我不需要每次测试前后都手动flush数据库
* 更进一步完善js覆盖率的检测体制
* 把FS_COVER这个config去掉,使得可以自动检测?
* 注意,TAG_DB_MODIFY的意思是这个TC可能修改数据库,而不是指它执行完以后会不会改变数据库。为了避免在TC执行到一半的时候fail,结果意外改变数据库,任何在执行过程中可能改变数据库的TC都应该打上TAG_DB_MODIFY。
23 changes: 23 additions & 0 deletions doc/test_front_coverage.md
@@ -1,5 +1,6 @@
# 基本流程
0. 我们接下来会要启用local-storage,但在这个模式下生成的jscoverage.js文件是存在bug的。我们需要先用jscover在非local-storage模式下生成一个jscoverage.js,然后复制,找个地方保存下它,再继续操作。
* 之所以需要启用local-storage,是为了让jscover的覆盖率变量在网页之间能够被共享,否则当我们切换页面的时候,旧页面的覆盖率变量就会丢失。
1. jscover加工前端代码:使用filesystem模式,生成加入插桩代码后的前端代码,注意要启用local-storage模式。
2. 将之前保存下来的非local-storage模式下的jscoverage.js拿出来,覆盖掉生成的local-storage模式下的jscoverage.js。
3. 修改nginx的conf文件,使代理插桩后的前端代码(需要杀光nginx进程才能使得更改conf文件有效)
Expand All @@ -13,6 +14,28 @@
7. 使用edge打开jscoverage.html
* 不能使用chrome,因为chrome禁止本地的文件访问。当然好像存在选项改,但还是直接Edge方便。

# 封装后的详细流程
1. 使用jscover插桩前端代码,示例命令:
`java -Dfile.encoding=UTF-8 -jar test/JSCover-all.jar -fs --local-storage --no-branch --no-function --include-unloaded-js --no-instrument=/front_end/lib D:/code_concerned/ruangong/rateMyCourse_front D:/code_concerned/ruangong/rateMyCourse_front_coverage`
* `Dfile.encoding=UTF-8`:这是为了避免插桩后的代码中的中文变成乱码
* `-jar test/JSCOVER-all.jar`:指定jscover的jar包位置,可以从JSCOVER官网下载,这里我已经放了一个在这了。
* `-fs`:指定为文件模式,也就是会生成插桩后的代码文件,其他模式见官网。
* `--local-storage`:指定使用HTML5的local-storage功能来跨页面保存覆盖率变量。
* `--no-branch --no-function`:禁止检查分支覆盖率和函数覆盖率。这是为了效率考虑,不然太慢了。
* `--include-unloaded-js`:使得初始的jscoverage.json中包含未加载的js文件的覆盖率信息。但因为我们之后肯定会用merge后的jscoverage.json文件覆盖它,所以是没啥用的一句话,不用管,但最好别删。
* `--no-instrument=/front_end/lib`:指定哪些js文件不应被进行插桩。这里我是指定的第三方代码全都不插桩。
* `D:/code_concerned/ruangong/rateMyCourse_front`:原代码
* `D:/code_concerned/ruangong/rateMyCourse_front_coverage`:插桩后的代码的目录

2. 运行测试样例
3. 合并插桩记录,示例命令:
`java -cp test/JSCover-all.jar jscover.report.Main --merge test/coverage/front/* D:/code_concerned/ruangong/rateMyCourse_front_coverage/`
* `-cp test/JSCover-all.jar jscover.report.Main`:我也不知道啥意思,反正就是指定要使用的java类吧。
* `--merge`:指明是要对插桩记录进行合并
* `test/coverage/front/*`:未合并的插桩记录文件的所在目录,应于front_config中的COVERAGE_DIR一致
* `D:/code_concerned/ruangong/rateMyCourse_front_coverage/`:插桩后的代码的目录,也是合并后的插桩记录文件的存放位置。我们需要放到代码目录里去,以从源代码级别查看哪些行被覆盖到了。

4. 使用edge打开插桩后的代码的目录下的jscoverage.html,查看结果。

# 不使用Proxy的原因
因为Proxy模式需要修改浏览器代理。而我们目前的本地测试环境使用了Fiddler作为浏览器代理,这两个代理也就冲突了。
8 changes: 7 additions & 1 deletion front/cover_saver.py
@@ -1,5 +1,5 @@
import os
from .front_config import COVERAGE_DIR
from test.front.front_config import COVERAGE_DIR, MUST_FS_COVER

class CoverSaver:
def __init__(self):
Expand All @@ -8,6 +8,12 @@ def __init__(self):
os.mkdir(COVERAGE_DIR)

def trySaveCoverageReport(self, driver, name=None):
has_jscover = driver.execute_script("return (typeof jscoverage_serializeCoverageToJSON) != \"undefined\"")
if not has_jscover:
if MUST_FS_COVER:
raise Exception("websites' code must be instrumented by JSCOVER since you have set MUST_FS_COVER true.")
return

json_str = driver.execute_script("return jscoverage_serializeCoverageToJSON();")

sub_dir = str(self.count)
Expand Down
4 changes: 2 additions & 2 deletions front/front_config.py
@@ -1,8 +1,8 @@
USING_HTTPS = True

# Use JSCover
FS_COVER = True
COVERAGE_DIR = "test/coverage/front/"
MUST_FS_COVER = False # If this is set true, error will be raised when we cannot detect jscover's instrumented code
COVERAGE_DIR = "test/coverage/front/" # The temp dir for jscover's unmerged coverage file

# We use Fiddler as our proxy, which is conflict with using JSCover as the proxy.
# So unless we can set two levels of proxy, we will never be able to use JSCover's proxy service.
Expand Down
4 changes: 1 addition & 3 deletions front/template/front_basic.py
Expand Up @@ -5,7 +5,6 @@
from selenium.webdriver import DesiredCapabilities

from test.front.cover_saver import cover_saver
from test.front.front_config import FS_COVER, PROXY_COVER

TAG_DB_MODIFY = "db_modify"

Expand All @@ -18,8 +17,7 @@ def setUpClass(cls):

@classmethod
def tearDownClass(cls):
if FS_COVER:
cover_saver.trySaveCoverageReport(cls.driver, name=cls.__name__)
cover_saver.trySaveCoverageReport(cls.driver, name=cls.__name__)
cls.driver.close()
super().tearDownClass()

Expand Down

0 comments on commit b56948d

Please sign in to comment.