如何使用Junit对Cordova插件进行单元测试? [英] How to unit test a cordova plugin using Junit?
问题描述
我已经做了一些Cordova插件,我想在上面编写单元测试。这个想法是,例如,当我运行cordova build android之后,测试文件将被移动到Android上的正确文件夹中,并且我可以使用Java运行测试。
I've done some Cordova plugins and I would like to write unit tests on it. The idea is that, after I run cordova build android, for example, the test files would be moved to the correct folder on Android and I could run tests with Java. Is that possible?
我已经看到一些插件可以测试Cordova插件,例如cordova插件测试框架。这里的问题是它仅测试呼叫和回调。例如,无法模拟对象。例如,我有一个可跟踪GPS的插件,我想对其进行测试。
I've seen some plugins to make possible to test a Cordova plugin, like cordova plugin test framework. The problem here is that it only tests the call and the callback. It's not possible to mock objects, for example. For instance, I have a plugin to track GPS and I would like to test it.
我也想对iOS方面进行测试。
I also would like to test the iOS side as well.
推荐答案
您在研究中正确地认为,当前对Cordova Core插件的测试都使用 cordova-plugin-test-框架
来实现在设备上运行的测试,例如 cordova-paramedic
或 cordova-mobile-spec
(有关如何触发构建的信息,请参见GitHub存储库中的CI配置)例如,使用 cordova-paramedic
)。
You were right in your research that the current tests for the Cordova Core plugins are all using cordova-plugin-test-framework
to implement tests that are run on the device with e.g. cordova-paramedic
or cordova-mobile-spec
(see the CI configuration in the GitHub repositories on how to trigger a build with cordova-paramedic
for example).
唯一的用 Java编写的标准单元测试的Cordova项目。平台使用的是Android和iOS的Obj-C。以Android为例,该文件夹存在: https://github.com/apache/ cordova-android / tree / master / test 这些测试可以通过 run_java_unit_tests.js
运行,也可以通过npm以 npm的形式运行运行java-unit-tests
。
The only Cordova projects where "normal" unit tests, written in Java for Android and Obj-C for iOS, are used are the platforms. For Android for example this folder exists: https://github.com/apache/cordova-android/tree/master/test These tests can be run via run_java_unit_tests.js
, which is also available via npm as npm run java-unit-tests
.
我很确定可以将相同的概念应用于插件测试。
(请让我知道是否可以尝试,并且它确实有效!)
I am pretty sure the same concept could be applied to plugin tests.
(Please let me know if you try and this actually works!)
这篇关于如何使用Junit对Cordova插件进行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!