简体   繁体   English

Javascript单元测试

[英]Javascript Unit test

I have a javascript app within 250 lines and want to add tests to that. 我在250行内有一个javascript应用程序,想要对此添加测试。 Whenever i make a tiny change, i have to run tests for atleast 10 cases manually, which i want to automate. 每当我进行微小的更改时,我都必须手动运行至少10种情况下的测试,这些测试要实现自动化。

I could have gone for frameworks as suggested in different posts, but the solution i want is minimum friction and codebase. 我本可以按照其他职位的建议去使用框架,但是我想要的解决方案是最小的摩擦和代码库。 Something like a single file for unit testing may work. 像单个文件那样进行单元测试的东西可能会起作用。

Is there a way for using JS testing without any frameworks ? 有没有一种方法可以使用没有任何框架的JS测试? I want to write Unit tests / functional tests both. 我想同时编写单元测试/功能测试。 Or if Frameworks is the only option, what frameworks are preferred in terms os ease of plugin to existing code + learning curve. 或者,如果“框架”是唯一的选择,则从易于插入现有代码+学习曲线的角度来看,首选哪种框架。

I would go with a framework. 我会考虑一个框架。 While your use case might be simple now, you might find yourself in a project where you need more functionality down the road. 尽管您的用例现在可能很简单,但是您可能会发现自己处于一个需要进一步功能的项目中。 You should be familiar with at least one testing framework/library for any language you use seriously. 对于您认真使用的任何语言,您应该至少熟悉一个测试框架/库。 The same goes for build systems and package management. 构建系统和程序包管理也是如此。

For javascript, I've only used mocha . 对于javascript,我只使用了mocha It's pretty comprehensive, easy to learn, can be used in browser or with node, and tests actually look really clean and easy to read. 它非常全面,易于学习,可以在浏览器中或与节点一起使用,并且测试实际上看起来非常干净且易于阅读。

Also, with mocha, you can choose your own assertions library, I use chai . 另外,通过mocha,您可以选择自己的断言库,我使用chai It's built with tdd and bdd in mind, and it makes writing assertions feel more like writing natural sentences than code. 它是在考虑tdd和bdd的基础上构建的,它使编写断言比编写代码更像编写自然语句。 Of course, you can still use whatever default assertion facilities your js environment gives you, if any. 当然,您仍然可以使用js环境为您提供的任何默认断言功能(如果有)。

its really hard to setup automated unit tests with plain javascript without any framework. 在没有任何框架的情况下,使用纯JavaScript很难设置自动化的单元测试。 if you try to reinvent the wheel of existing javascript unit testing framework yourself, that might be huge effort than your 250 lines of code for which you are writing test. 如果您自己尝试重新发明现有的javascript单元测试框架,那可能比编写测试的250行代码要付出巨大的努力。

so if you are going for an framework, jasmine and qunitjs are promising unittesting framework for javascript. 因此,如果您要使用框架,那么jasmine和qunitjs是有前途的javascript单元测试框架。

http://jasmine.github.io/ http://jasmine.github.io/

https://qunitjs.com/ https://qunitjs.com/

Unit Testing frameworks are helpful because the provide environments that will run the tests and give you feedback about them. 单元测试框架很有用,因为提供的环境将运行测试并为您提供有关它们的反馈。 To not use one would be to take implementation of those responsibilities on yourself and would be much more work than it sounds like you are trying to do. 不使用一个人将是对自己承担这些责任,并且将比您试图做的工作多得多。

If you are using node, setting up a unit testing framework is very easy. 如果您使用的是节点,则设置单元测试框架非常容易。 I like to use Karma as a test runner with the mocha testing framework and chai assertion library. 我喜欢将Karma用作mocha测试框架和chai断言库的测试运行程序。

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

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