📜 ⬆️ ⬇️

Testing is not for beginners

I am a big fan of testing. I write about it in a blog and mailing list, I discuss it with other developers in my spare time, I have gone so far that I even created a training course on testing at Go.

But despite all my love of testing, I do not recommend it to beginners.

It sounds crazy, right? In this article I am going to explain my point of view in more detail, but the whole point, in the end, boils down to two points:
')
  1. Beginners do not have enough knowledge to write anything but the most simple tests. This inevitably leads to the next point ...
  2. Trying to train the skills necessary to write realistic tests while teaching programming is extremely difficult.

I understand that this is, in principle, one point. In any case, I broke it in two in order to make it easier to understand.

I know many of you will disagree with me, but please read the article, and if after reading you remain unconvinced, I will be happy to discuss this with you. After all, I'm here to study.

Beginners do not have enough knowledge to write anything but the most simple tests.


Whenever a newbie writes code, his main goal is not to decompose tasks, avoid global variables, or write test code. Honestly, most newbies probably have little idea what all this is. In fact, their main purpose is simple - to make this damn thing work. Everything.

Confirmation of this is unlikely to require any effort, just look at the code written by a beginner.

Web app on go? Surely they write SQL queries everywhere and everywhere in the code, and almost guaranteed the connection to the database is stored in a global variable.

Rails application? Surely you will see business logic in views and tons of logical traffic jams in controllers.

PHP web application? I would not be surprised if ALL the logic will be in one single php file - parsing of forms, interaction with the database, etc.

Even if we take something elementary, say, a calculator with limited functionality, we still come across examples like those described by me. This is not at all due to the fact that they do not care, they just don’t know how to do better.

Beginners do not know what dependency injection is, do not understand how global variables complicate testing, most likely they don’t know what a mock is, so it’s rather ridiculous to expect them to understand how to design a code that is easy to lock.

As a result, only the simplest tests like these make sense for a beginner:

func Add(a, b int) int { return a+b } // And a test... func TestAdd(t *testing.T) { got := Add(2, 4) want := 6 if got != want { t.Errorf("Add() = %d; want %d", got, want) } } 

Although I have no problem to show this to beginners and give them an idea of ​​what testing is, I think it’s rather ridiculous to show them this code and pretend that this is something like a real test.

In the end, this leads us to start teaching them more. We try to explain what dependency injection is, why global variables complicate testing, as time.Now () can make it difficult to check border cases. And in this place I'm starting to worry, because we no longer teach the beginner how to write code. At this moment we teach him how to write code AND HOW TO TEST at the same time. And that brings me to the second point ...

Trying to train the skills you need to write realistic tests while learning programming is extremely difficult.


As before, I want you to think about code written by a newbie, but this time I want you to remember some of the first programs you wrote.

If you were like me, your first web application might look something like this:

 <p> <?php // This may not work. I don't know PHP anymore. $name = $_GET['name']; echo "Hello, " . $name; ?> </p> 

A work of art, right?

Now imagine that you just wrote this code for the first time and someone tells you that you should test it. And you must use React. And the framework. Oh, and you would do well to set up a database and, possibly, GraphQL to work with it.

I don’t know why, but we, the developers, have a habit of using the skills gained through many years of experience and practice and expecting others, especially beginners, to do the same . It's just ridiculous! This is how to expect from someone that he will understand mathematics only because you yourself have already studied trigonometry, algebra and much more, and you can use mathematics to solve specific problems.

If something works well for you, it does not mean that it is suitable for beginners. They may be out of context, without the experience and practice necessary to take advantage of what you use. Or, perhaps, the problems over which they are fighting are in fact too simple to use all these complex solutions.

As if we forgot how we studied step by step how HTTP requests work. How headlines work. Cookies Forms. How does the POST request to the server work - or even that there are ALL HTTP-methods IN GENERAL. And all this we learned the good old method of trial and error.

In fact, I don’t think that tests are to blame for everything, the real problem is that we firmly believe - you have to learn programming, testing, website building and a million other things at the same time. I don’t really know how it happened, but I suspect in part the problem is that we don’t take the trouble to think about what it would be worth studying all of this. A novice asks “what should I learn?” And we start “teach testing, react, graphql and go, but use only the standard library ...”

No no and one more time no. Just stop.



This is ridiculous, since in other conditions the absurdity of such an approach is obvious. If you want to teach someone to play football, you will start with the basics such as rallying and dribbling the ball. You do not start by saying “Here's how the professionals do it”, turn on the video with Ronaldo and leave. So why the hell are we doing this to newbies in programming?

We are trying to justify ourselves by saying “well, of course, they understand that it is impossible to learn all this at the same time,” but they do not understand! The situation is aggravated by the fact that novice developers, falling into this trap, feel like shit because they are stuck. It seems to them that they simply do not have something that is needed to become a developer, and this is a shame for you and me, because many of them would love programming, if they didn’t crash into this brick wall.

And this brings me to what I actually wanted to say - many of us learn better if we focus on studying several things at once. We want new challenges, we want to try new things, but we don’t want to get confused and fall into a stupor. Studying testing and more — for example, how to create a web application, how http, cookie, etc., works — is an easy way to get into this trap. As a result, I usually recommend first learning the rest, and testing last. You can always go back to your old projects and see how you would rebuild them now when you studied testing, but this is possible only if you are not overloaded, not upset and, in the end, have not given up.

But what if I want to learn how to test ?! (and a million others "what if")


Cool, go ahead! Of course, you can first study testing, and only then web development or some other topic. I’m sure some have done just that, and you might like this way more. When I say that testing is not for beginners, I do not say that this is a bad topic to study, I mean, that trying to study testing plus something else is a mistake.

Most people want to first learn how to create web applications or something visual, but this does not mean that you need to start with this, you may well start with testing. Probably, you will not begin to understand anything until you have crammed the cones, but do not let me stop you from learning what you want.

It also does not mean that you cannot learn while doing pair programming or something like that. With a mentor, you can learn many things at the same time and successfully withstand this load, because you have someone who will lead you through all this. In this situation, if you get lost, you will not fall into a dead end and you will not feel like a loser, because the person who says “You are doing great, is just there, it’s just too difficult for now. Next time, try X and Y instead! ” Simply put, in this scenario, you will not get confused, disappointed and quit everything.

Source: https://habr.com/ru/post/451118/


All Articles