javascript测试_用javascript测试

javascript测试

How Writing Tests for Your Code Can Help Improve Your Code

为您的代码编写测试如何帮助您改善代码

An important part of the process of writing clean, effective code is writing and running tests for your code. As a relative newcomer to the field of software engineering, the thought of writing tests for your code might initially seem intimidating and complicated. You know you should do it, but you’re not sure of where or how to start. You might think writing tests for your code is time-consuming and unnecessary, but quite the opposite is true — implementing tests helps us to spot any bugs before we deploy our code and allows our programs to run smoother, ultimately resulting in saved time and a more professional, efficient end product.

编写干净,有效的代码过程中的重要部分是为代码编写和运行测试。 作为软件工程领域的一个相对较新的事物,为代码编写测试的想法一开始似乎是令人生畏和复杂的。 您知道应该这样做,但是不确定从哪里开始或如何开始。 您可能会认为为代码编写测试是耗时且不必要的,但事实恰恰相反-实施测试有助于我们在部署代码之前发现所有错误,并使我们的程序运行更加流畅,最终节省了时间并节省了时间。更专业,高效的最终产品。

A good practice is to adhere to test-driven development (TDD). In TDD, we first write tests for our code, keeping in mind what we want our code to ultimately do, write some initial code to pass the tests, and then refactor our code to make the code clean and as DRY as possible. We can think of this process as red, green, and refactor. When we initially write our tests, the tests will fail, giving us red lights. We then write enough code to make our tests pass, turning the lights green. Next, we refactor and clean up our code, which might then break some of our tests, so we repeat the whole process as necessary. The below diagram is helpful in illustrating the concept:

一个好的做法是坚持测试驱动开发 (TDD)。 在TDD中,我们首先为代码编写测试,牢记我们希望代码最终完成的工作,编写一些初始代码以通过测试,然后重构代码以使代码整洁并尽可能干燥。 我们可以将此过程视为红色,绿色和重构 。 当我们最初编写测试时,测试将失败,并为我们提供红灯。 然后,我们编写足够的代码以使测试通过,并使指示灯变为绿色。 接下来,我们重构并清理我们的代码,这可能会破坏我们的一些测试,因此我们在必要时重复整个过程。 下图有助于说明这一概念:

Image for post
Marsner 马斯纳

What are some different types of tests?

有哪些不同类型的测试?

While there are many different types of tests, we’ll cover three of the most common and useful ones for web development here.

尽管有许多不同类型的测试,但在这里我们将介绍三种最常用和最有用的Web开发测试。

  • Unit Tests

    单元测试
  • Integration Tests

    整合测试
  • End-to-End (E2E) Tests

    端到端(E2E)测试

Unit tests involve testing one individual piece of our code, such as a single function or a class, at a time. Unit tests are fully isolated and check that given an input, the output is what we expect it to be.

单元测试涉及一次测试我们的代码的单个部分,例如单个函数或一个类。 单元测试是完全隔离的,并检查给定输入,输出就是我们期望的结果。

In unit testing, it is good practice to separate tests into a dedicated file. Below are common naming patterns used:

在单元测试中,优良作法是将测试分成专用文件。 以下是常用的命名模式:

  • {file_name}.test.js

    {file_name} .test.js
  • {file_name}.spec.js

    {file_name} .spec.js

The below code snippet is an example of of a unit test for the specified generateText function:

以下代码段是针对指定generateText函数的单元测试的示例:

In comparison, integration tests look at how functions interact and work together. This involves dependencies.

相比之下, 集成测试则着眼于功能如何相互作用和协同工作。 这涉及到依赖性。

E2E tests look at the full flow of the code and imitates a real user experience. This involves looking at DOM manipulation based on what a user does on the screen.

E2E测试会查看代码的完整流程,并模仿真实的用户体验。 这涉及根据用户在屏幕上的操作来查看DOM操作。

As a general rule, unit tests are less complex and costly, thus we usually end up writing unit tests with more frequency. The idea is that if all of the individual pieces of our code are working as expected, then we have a good foundation for how our code will work as a whole. We should always use unit tests and then use integration and E2E tests as needed.

通常,单元测试不那么复杂且成本较高,因此我们通常最终会以更高的频率编写单元测试。 这个想法是,如果我们的代码的所有各个部分都按预期工作,那么我们就为我们的代码如何整体工作奠定了良好的基础。 我们应该始终使用单元测试,然后根据需要使用集成和E2E测试。

Image for post
credit) 信誉 )

Testing Frameworks

测试框架

While it is possible to write your own tests from scratch, there are many testing frameworks that exist that make testing much easier, more efficient, and more reliable.

尽管可以从头开始编写自己的测试,但是存在许多测试框架,这些框架使测试变得更加轻松,高效和可靠。

Below are a few of the most popular ones for testing JavaScript, with links to the documentation:

以下是一些最流行JavaScript测试工具,并提供文档链接:

翻译自: https://medium.com/swlh/testing-in-javascript-aa32e51d55ec

javascript测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值