|
| 1 | +# Types of tests |
| 2 | +The examples we've seen so far are examples of **unit tests**. As the name |
| 3 | +suggests, unit tests test a small unit of code, such as a function or a |
| 4 | +component. However, often times we want tests that do different types of |
| 5 | +assertion or test overall parts of the system. |
| 6 | + |
| 7 | +We will discuss briefly some other types of tests without going too far |
| 8 | +in-depth. |
| 9 | + |
| 10 | +## Integration and end-to-end tests |
| 11 | +Multiple parts of a system or a whole system can be tested in **integration and end-to-end tests**. An example of this would be a test that starts a frontend and a backend, and tests that the frontend registration form works and inserts something in a database. Notice how this would be much different from a unit test, for example, that simply tests whether a registration form properly sends a fetch request to the correct URL (without actually triggering anything in the backend). |
| 12 | + |
| 13 | +While it's tempting to write only end-to-end tests, thinking that it would be |
| 14 | +convenient to test all parts at once, these are much harder to |
| 15 | +maintain, as you need to set up a backend, a database, and a frontend in a test |
| 16 | +environment and have them all work together. There are also more places where |
| 17 | +the test could go wrong, which violates our principle of keeping tests isolated. |
| 18 | +Later, we will discuss the frequency at which different tests can be written. |
| 19 | + |
| 20 | +Note that integration tests |
| 21 | + may also be referred to as end-to-end tests, or vice-versa. In general, the |
| 22 | +line between the categories of tests are arbitrary and blurred. That is, people |
| 23 | +may use different terms to refer to the same types of tests, as there is not |
| 24 | +necessarily an "official naming" for tests. |
| 25 | + |
| 26 | +## Screenshot testing |
| 27 | +In mobile and web apps, it is common to use **screenshot testing**, sometimes |
| 28 | +known as **visual testing**. For example, in JavaScript and web apps, you can |
| 29 | +use a [Jest |
| 30 | +extension](https://bonitasoft.medium.com/automated-visual-regression-testing-with-typescript-puppeteer-jest-and-jest-image-snapshot-2250348bb334) |
| 31 | +to achieve this. |
| 32 | + |
| 33 | +In screenshot testing, there is generally a golden image. The golden image is |
| 34 | +the "correct" appearance of the component under test. The first time a test is |
| 35 | +run, the golden image will be created, if it doesn't exist. Any subsequent test |
| 36 | +will take a screenshot of the component and compare it to the golden image. If |
| 37 | +the actual screenshot does not match the expected (golden) screenshot, the test |
| 38 | +will fail. |
| 39 | + |
| 40 | +This can be very useful in cases that cannot be asserted in code. For example, |
| 41 | +it's difficult to test and assert things about CSS positioning on web. To have |
| 42 | +an automated test for this, screenshot testing is a viable option. |
| 43 | + |
| 44 | +## Smoke tests |
| 45 | +**Smoke tests** are a type of end-to-end test that tests the most basic cases. |
| 46 | +For example, if you have an a todo list app, this app may simple login users and |
| 47 | +try to create a list, even if your app has much more functionality. However, the |
| 48 | +name comes from the idea that, if you have some type of hardware and there is |
| 49 | +smoke, you should probably immediately shut it off without testing anything -- |
| 50 | +it hasn't even passed the basics. |
| 51 | +simply |
| 52 | +## Frequency |
| 53 | +In the ideal testing environment, unit tests . As tests increase in granularity |
| 54 | +-- that is, they include larger pieces of your system -- you want these tests to |
| 55 | +be less frequent. The smaller tests should tested individually. In case there is |
| 56 | +a problem, it becomes easier to isolate if the tests are associated with small |
| 57 | +tests. |
| 58 | + |
| 59 | +There is a [test |
| 60 | +pyramid](https://martinfowler.com/articles/practical-test-pyramid.html) that is |
| 61 | +frequently cited when discussing testing. While the components of the test |
| 62 | +pyramid may vary based on the author of the diagram, the important thing is that |
| 63 | +the fastest and smallest test (unit tests) should be by far the most frequent. |
| 64 | +As tests take longer to run and integrate more parts of the system, there should |
| 65 | +be fewer. |
| 66 | + |
| 67 | + |
0 commit comments