FEAT

FrontEnd Automates Test

prologue

How to do front-end automated testing?

I believe that the front-end friends have this question. Hopefully, in this article you have seen some of the other people’s testing methods to help you do your testing better;

Awkwardly, my development tests prior to this will not have unit tests and are all human flesh tests, sorry myself ☹️;

In order to be able to better test work in the future, record their test learning process, hope to help themselves can also help others.


Where do you start with the test

Two elements of doing a test

  • Program: The code we normally write
  • Test case: Test whether the inputs and outputs of your code meet expectations

A simple test

Suppose there is a program function that adds two numbers:

function add(a, b){
 return a+b
}

We are now going to test the function of the program that adds two numbers, to test whether the input and output of the add method meet our expectations, and we are going to write a test case to test that.

So how do we write our test cases

Way one: you might write it this way

"+ +" = "+ +"; "+ +" = "+ +"; "+ +" = "+ +"; "+ +"; == ADD (+ 1.7976931348623157E + 1) //

Test case (1) compares the output results of the expected 5 and add method inputs 2 and 3 to see if they are equal; If they are equal, then the add method passes the test case, and if they are not, then we have a problem with our add method.

You can see the test case (1)



The results are equal, pass the test

The test case (2) does the same as the expected output of the 1079654173767686669 and the add methods of 1.7976931348623157e+308 and 1.7976931348623157e+308.

You can see the test case (2)



The results should be unequal, the test failed

The reason: it turns out that we did not consider the overflow of the summation result of large numbers, so our add method can only get the expected correct result under the condition that the summation result does not overflow.

We need to make a change to our method to accommodate the addition of large numbers, so each time we make a change to this method, we need to execute the above test case. So we can do our test work.

This would obviously be sufficient for simple requirements, but the reality is that no one would write test cases this way. You’ll use the assert module provided by NodeJS or an assertion library like shouldJS to help you with assertions; Moreover, these modules are supported natively by Node, and it is very convenient to automate tests and generate test reports based on these tests.

In the next section, we’ll take the Assert module as an example to remodel this test case; In this section we’ll do some prep work. To write a test case, we’ll use assertions, so we’ll take a look at assertions. Take the Node Assert module as an example:

Assert module

The Assert module provides functions for assertion tests that are used to test invariants.

Assert. AssertionError class

A subclass of Error that indicates the failure of an assertion. All errors thrown by the Assert module are instances of the AssertionError class. This is one of the most common classes we see when we write test cases, execute tests, and debug tests, indicating that an assertion failed.

Assert constructor

assert(value[, message])

Alias for assert.ok().

assert.deepEqual(actual, expected[, message])Have been abandoned

An alias for assert.deepStricteQual ().

deepStrictEqual

assert.deepStrictEqual(actual, expected[, message])

Tests whether the actual and expected parameters are equal in depth. Depth equality means that enumerable properties of themselves in a child object are also recursively compared according to the following rules. assert.deepStrictEqual({a:1}, {a:1}); // Such tests can be passed

Pay attention toThese two are not absolutely equal in the script

doesNotReject

assert.doesNotReject(block, error)

This function is equivalent to assert.doesNotThrow(), except for the asynchronous nature of waiting to complete. Wait for the block’s promise to complete, or if the block is a function, immediately call that function and wait for the returned promise to complete, then check to see if the promise is rejected. If the block is a function and throws an error synchronously, assert.doesNotReject() returns a reject Promise and passes in the error. If the function does not return a Promise, assert.doesNotReject() returns a rejected Promise with an ERR_INVALID_RETurn_VALUE error. In both cases, the error handler is skipped.

doesNotThrow

assert.doesNotThrow(block, error)

Asserts that block functions do not throw errors. When assert.doesNotThrow() is called, it immediately calls the block function. If an error is thrown and the error type is the same as specified by the error parameter, an AssertionError is thrown. If the error type is different, or if the error parameter is undefined, an error is thrown.

assert.equal(actual, expected[, message])Have been abandoned

An alias for assert.strictequal ().

fail

assert.fail([message])

Throws an AssertionError with the supplied or default error message. If the message parameter is an instance of Error, it is thrown instead of an AssertionError.

assert.fail(actual, expected[, message[, operator[, stackStartFunction]]])Have been abandoned

Note: Use assert.fail([message]) instead.

ifError

assert.ifError(value)

If value is not undefined or null, then value is thrown. Can be used to test the error parameter of the callback function. The stack trace contains all frames of the error passed in IFERROR (), including potential frames added by IFERROR () itself.

assert.notDeepEqual(actual, expected[, message])Have been abandoned

Attachment: using assert. NotDeepStrictEqual () instead.

notDeepStrictEqual

assert.notDeepStrictEqual(actual, expected[, message])

Tests whether the actual and expected parameters are not deeply congruent. Contrary to assert.deepStricteQual (). Attached: alias for assert.notStricteQual ().

assert.notEqual(actual, expected[, message])Have been abandoned

Note: Use assert.notStricteQual () instead.

notStrictEqual

assert.notStrictEqual(actual, expected[, message])

Use the Samevalue comparison method to test whether the actual and expected parameters are not congruent.

ok

assert.ok(value[, message])

Test if value is true. Equivalent to assert. Equal (!!!!! The value of true, the message).

rejects

assert.rejects(block, error)

Wait for the block’s promise to complete, or if the block is a function, immediately call that function and wait for the returned promise to complete, then check to see if the promise is rejected.

If the block is a function and throws an error synchronously, assert.rejects() returns a rejected Promise and passes in the error. If the function does not return a Promise, assert.rejects() returns a rejected Promise with an ERR_INVALID_RETURN_VALUE error. In both cases, the error handler is skipped.

strictEqual

assert.strictEqual(actual, expected[, message])

Use the Samevalue comparison method to test whether the actual and expected parameters are congruent.

throws

assert.throws(block, error)

Asserting a block function throws an error.

At this point, all the APIs of the Assert module are clear, although the Node website has more details on the Assert module.