DEV Community

Cover image for Learning a Language by Writing Unit Tests
Itachi Uchiha
Itachi Uchiha

Posted on

Learning a Language by Writing Unit Tests

Hi there!

In this post, I'll talk about a boring topic. It's about learning new technology.

In general, each developer can learn programming languages in different ways.

For example, you want to learn the German language. Some people can do that by creating some codes to remember words.
It should be the same as for programming languages too.

Language learning

For example, you're a backend developer who uses C# for your projects but you want to learn Golang or Rust. But you don't have any idea about their concepts such as grammar or rules.

If I talk about myself, I could learn a language while developing basic web application backends. It's all about you!

It's Important to Gain a Unit Testing Habit

At the end of the day, whatever programming languages we work with, we may want to write tests if we are suspicious about our code's behavior. Don't worry this is completely normal.

For example, you write a function that returns a float value. But, it can return always integer values. This is an unexpected situation for your flow.

Because of this, we can write unit tests. We can do the same thing while we learn a new language to adapt to the unit test concepts of this language. You will also have the ability to write unit tests in any language. It will be a habit for you :) For example, I work like that;

testproc test_a_function:

     assert!(a_function(), "a string");
Enter fullscreen mode Exit fullscreen mode

The above code is meaningless for you. Even for me :)

I want to make you focus on the unit tests without code. It this possible? Yes, it's. Think like it's a feature that waiting to be completed. There is no function a_function yet. But we know it should return "a string". We'll develop our function to return "a string".

I know real-world problems are very difficult than that.

Now, I'm starting to write a function to run the above test;

proc a_function:
   return "aa string";
Enter fullscreen mode Exit fullscreen mode

I guess my test result will fail. So, is it a bad thing for me? I don't think so :) I prefer errors on unit tests instead of the production environment.

Of course, we can't handle every situation. But our goal is to increase coverage results.

Learning Process

Let's come up with the learning process. I used Rust for this post. I'm not new at Rust. I wrote posts on this platform before. I preferred Rust because it's a memory-safe and thread-safe language.

How I Write Unit Tests in Rust

Actually, there are tips on this post. But I would like to explain.

I write tests before writing functions. So, while there weren't functions, I write unit tests.

You can create libraries or executable projects that have the main function. I'm doing the second one.

I always want to see outputs by stdin. So, I create a normal project :)

When you create a project in Rust, you'll see a code piece like that;

fn main() {
    println!("Hello, world!");
}
Enter fullscreen mode Exit fullscreen mode

I'm immediately starting to write a unit test below the main function. Let's write a simple unit test like that;

#[cfg(test)]
mod tests {
    use crate::first_function;

    #[test]
    fn message_is_validate() {
        assert_eq!(first_function(), "Hello, world!");
    }
}
Enter fullscreen mode Exit fullscreen mode

I don't care what is the cfg attribute for now. But, I can say it's doing conditional checking.

If I want to run tests using cargo, it won't run tests because there is no function called a_function

cargo test

the output will be like that;

^^^^^^^^^^^^^^ not found in this scope

Let's implement the a_function.

fn first_function() -> &'static str {
    "Hello, world!"
}
Enter fullscreen mode Exit fullscreen mode

Now, we can run tests again :)

cargo test

We see the "ok" value, which is every developer's dream :)

running 1 test
test tests::message_is_validate ... ok


test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Enter fullscreen mode Exit fullscreen mode

Of course, the tests of stochastic processes are not that simple :) But I can simply say that this is how I learned a language. Do not say that you are an expert or something. Expertization in something and trying to understand it is two different things. I'm doing the second one :)

Conclusion

This kind of learning method gives me unit test awareness. Of course, everyone can learn in different ways. But when it is unavoidable in your unit testing projects and you don't write unit tests, bigger problems may show up.

Because of this, I think it would be beneficial for you to acquire unit test awareness in a way, even if it is not with this kind of learning. Maybe the way you learn a language is to write a test on the existing function.

Resources

Rust Programming Language

Thanks for reading!

Top comments (8)

Collapse
 
basman profile image
Adam.S

You can also call cargo test — —nocapture if you ever need to see the actual output.

Collapse
 
itachiuchiha profile image
Itachi Uchiha

I didn't know this before. I'll try. I think I should learn also compiler and tool flags :P

Collapse
 
basman profile image
Adam.S

I have only recently used it myself :)

Thread Thread
 
itachiuchiha profile image
Itachi Uchiha

Have you ever used this command with Intellij? If so, how can I set this flag?

Thread Thread
 
basman profile image
Adam.S

No sorry. Just at the command line.

Collapse
 
admindashboards profile image
admin-dashboards

This is a good advice.

Collapse
 
mcsee profile image
Maxi Contieri

Amazing !

You should definitive try TDD

Collapse
 
itachiuchiha profile image
Itachi Uchiha

Thanks :) actually, I'm doing TDD but TDD also helps me in my learning path :)