Skip to content

Commit f3f2507

Browse files
committed
write first two sections
1 parent eb3b380 commit f3f2507

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# What is a test?
2+
In programming, a test refers to a piece of code that can be run to verify the
3+
behavior of a program. For example, if you write a function, and you want to be
4+
sure that it works for various inputs and outputs, you can write a test for this
5+
function.
6+
7+
Tests themselves are code. At larger companies, every piece of submitted code
8+
needs to submitted with a corresponding test. A general rule of thumb is that,
9+
if you change a line of your code so that the behavior is wrong, a test
10+
somewhere should fail, so that such a mistake can be caught.
11+
12+
Tests are an important part of many companies' infrastructure for development.
13+
Generally, humans are not manually running these tests, though you may manually
14+
run it before sending a PR or while working on the test. In a process called
15+
**continuous integration** (CI), these tests will automatically run after every
16+
pull request is proposed. This means that generally, if your code and pull
17+
request are not passing the tests, it cannot be merged in.
18+
19+
Suppose you had a function called `square(n)`, that is supposed to square a
20+
number.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Why is testing important?
2+
3+
You may think it is sufficient simply to manually test your code.
4+
5+
There are a few reasons testing is very important when it comes to a growing
6+
codebase. You should not imagine the situation where you are working on a
7+
project with one or two people, but a situation where you are working with a
8+
larger team and with code that the company will use the code long after you leave. You
9+
may not be at the company five years later, or you may forget what the code does
10+
in six months.
11+
12+
## Correctness
13+
Most obviously, a test is used to verify the correctness of the code. If you
14+
have ever changed your code then had to test three or more cases manually, then
15+
you are familiar with the fact that it is quite cumbersome to repeatedly check
16+
manually. Not only that, it's very easy to forget to check some case when a
17+
human is doing it by hand.
18+
19+
In contract, when you write a test, you simply need to run a command in order to
20+
initiate the tests again, rather than repeating some steps to ensure that your
21+
code is working.
22+
23+
If you work at a company with code review, consider the perspective of the
24+
person reviewing your code: how do they know your code doesn't have bugs? How do
25+
they know it works correctly? If your code is submitted along with tests, the
26+
reviewer can be aware of exactly what you have or haven't verified about your
27+
code.
28+
29+
## Ease of change
30+
Have you ever
31+
felt scared to change code, because you weren't sure if it would still work for
32+
all the cases after you changed it? With a good test suite, this fear generally
33+
doesn't exist. Even if the code is rewritten but the same tests pass, indicating
34+
that the new code has the same behavior, one can feel a lot more secure about
35+
rewriting the code.
36+
37+
Having good tests allows code to be changed in a more robust fashion. When the
38+
same tests pass, a developer can be more sure (perhaps not completely sure,
39+
depending on the tests) that changing the code did not break anything.
40+
Additionally, it allows people who are not familiar with the code (say, your
41+
teammates who work on related code, or someone who is responsible for your code
42+
five years later) to more easily work with the code.
43+
44+
## Documentation
45+
A well-written test suite serves as documentation. Again, imagine you have just
46+
joined a company, and you don't know what a function or component does. In fact, if you want to
47+
understand what a piece of code does, it's often more productive to go read the
48+
tests first, rather than the code itself.
49+
50+
In the next section, we'll look an
51+
example of a test, which will help illustrate how the tests themselves can be
52+
used to document the code.
53+

0 commit comments

Comments
 (0)