stackconf 2024 | Test like a ninja with Go by Ivan Presenti.pdf

NETWAYS 62 views 58 slides Jul 26, 2024
Slide 1
Slide 1 of 58
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58

About This Presentation

Not tested? Not done! Yet another talk about tests? I aim to present you with the techniques and tools you might use to build efficient and reliable tests. We’ll use Go, which provides a great testing experience. I’ll show you overlooked techniques such as benchmarking, fuzzing, etc. Plus, I’l...


Slide Content

Test like a
Ninja with
Go
Ivan ossan Pesenti
Software Developer
StackConf 2024

About ME
❖Name: Ivan Pesenti (AKA
“ossan”)
❖Age: 28
❖Country: Italy
❖Role: Software Engineer
❖Engagements: speaker, mentor,
technical writer, course author
❖Hobby: anime, tattoos, football

Road to Glory
When to Use, examples,
How to Run Them.


Why Testing, Clean Tests,
Kinds of Tests, Tests in
Go, etc.
When to Use, Mockery
pkg, testify/mock,
examples.
When to Use, examples,
How to Run Them.



When to Use, examples,
How to Run Them.
When to Use, examples,
How to Run Them, other
pkgs.




01
04
02
05
03
06
Tests Theory Unit Test Benchmarking
Fuzzing Example Integration

How many of you write
tests?

58%
Developers involved in writing automation tests.
Source: https://www.jetbrains.com/lp/devecosystem-2023/testing/

Testing Intro
01
One of the programming pillars.

Tests’ Reasons
Prevent them while
coding.
Enrich the
documentation of the
source functions.
Speeds up the
development process.
You can master the
programming
language you use.
Regressions
Speed
Documentation
Knowledge

Sharing
Moments

“When you have to fix a bug, hold
on. First write a test to reproduce it.
Finally, change the source code.”
—myself.

Clean Test
01
How does a clean test look like?

Characteristics
❖Meaningful test name
❖Follow the AAA paradigm:
➢ Arrange
➢ Act
➢ Assert
❖Follow the F.I.R.S.T principle
❖Asserts one behavior
❖Use meaningful test data

Kinds of Tests
01
Which tests can we use in our software?

Regarding
performance.
Explain the usage.
Test List
Assess one function.
Try to mess with your
code.
Assess a feature. Not-Exhaustive list.
Unit Benchmarks Examples
Fuzz Integration Others

Select the Right tool
for the right job

Tests in Go
01
Go & tests.

Go Peculiarities
❖No need for external frameworks
❖Standard Library is enough
❖Not included in the compiled binary
❖Black & White Box Testing
❖Easy usage of fixtures and test data

Executes
It runs them based on
the received
instructions.
Build
It scans all the test
files in the project.
Local Directory Mode
vs Package Directory
Mode.
Reports
Prints to the standard
output the outcome
of tests.
Scans
Go Test Runner

Testing package
Purpose
Make the Go test code interact with the Go’s Test
Runner.
Mandatory
It must be imported by all the test functions.
Without it, the function is not a valid test.
Exposed Types
The main exposed types are: testing.T, testing.B,
and testing.F

The Testify Library
Ease assertion
process.
Let you set a mock
up by assigning a
behaviour.
Let you group a
bunch of related
tests.
It enhances your
testing experience.
Assert
Suite
Mock
More…

Testing
Techniques
01
Table-Driven Tests and SubTesting.

Source Function

Table-Driven Tests

Subtesting

Unit Tests
02
To test smallest units of software.

When to Use
Speed
Scope
Complexity
●Mocks
●Stub
●Interfaces
●Scaffolded CodeUsefulness
Description
Assess that a single Unit Under
Test (UUT) behaves as
expected. Dependencies have
to be mocked.
Characteristics
My Experience
Key Players
Handful to test functions with
high cognitive load. They’re my
second choice since I prefer
writing Integration Tests.

It uses a CLI tool to
generate code.
Customizable
commands.
Mockery tool
Automatic mocks
generation, based on
interfaces.
It’s well integrated
with the
testify/mock pkg.
It mocks also
unexported
interfaces.

The testify.mock pkg
.On
Lets you start to set the mock’s behavior up.
.Return
Tells what values to return when invoked.
.Once
The mock should return the value only once. See also
(twice, times).
.AssertExpectati
ons
Makes sure that the mock behaves as per specifications.

Source Code

Test Code

Benchmarking
03
To measure performance and efficiency.

When to Use
Speed
Scope
Complexity
●testing.B
●B.N
●Execution Flags
Usefulness
Description
Measures the performance of a
function/method. It also checks
the consumed memory.
Running-machine dependant.
Characteristics
My Experience
Key Players
Useful for two implementations
of a feature or if you’d like to
learn more about performance
of an function.

Let’s Run it

Fuzzing
04
Screws up variables’ values.

When to Use
Speed
Scope
Complexity
●testing.F
●Seed Corpus
●Fuzz Target
●Execution FlagsUsefulness
Description
Find out new test cases that
might lead to crashes or
failures. Outcomes are not
predictable.
Characteristics
My Experience
Key Players
I’ve just used them for
strings-based functions.
Especially useful for dealing
with user inputs.

Example
05
Code as documentation.

When to Use
Speed
Scope
Complexity
●Example
●// Output: <some value>
●Test
Usefulness
Description
Convert test functions into
useful documentation. They can
also be run directly from the
browser.
Characteristics
My Experience
Key Players
Super useful if you’re
developing a third-party
package/library to help your
consumers.

Integration Tests
06
Test a component, not just a unit.

When to Use
Speed
Scope
Complexity
●testing pkg
●Build tags
●testify
●DockerUsefulness
Description
Test multiple UUTs together.
They use actual dependencies,
not mocked ones. They
indirectly exercise the same
logic as the unit tests.
Characteristics
My Experience
Key Players
I always write them, in any
layers of my applications (DB,
cache, web API call, and so on).
They are always the most
numerous.

What’s Missing?
Automatic container initialization.
Cleanup of resources between tests.
TearDown of the containers at the end.
Grouping and organization for tests.

testify.suite
The testify/suite package provides us with
testing groupings capabilities.

testcontainers-go
My go-to package for Integration Tests.

Key Features
Run containers based
on Docker images,
Dockerfile, and so on.
Terminate containers
no longer used.
Programmatically
access images,
containers, volumes,
etc.
Smoothen the
integration testing
experience.

Books/Courses
●https://www.amazon.com/Test-Driven-Development-practical-idiomatic-real-world-ebook/dp/B0B8SY6G96
●https://quii.gitbook.io/learn-go-with-tests
●https://www.educative.io/courses/learning-test-driven-development-with-go
Test & generic blog posts:
●https://hackernoon.com/how-to-debug-any-problem-ac6f8a867fae
●https://www.jetbrains.com/lp/devecosystem-2023/testing/
●https://tddmanifesto.com/a-clean-test/
Go related blog posts:
●https://medium.com/@opheliaandcat/table-driven-and-individual-subtests-in-golang-which-one-to-use-360b5de39d
●https://medium.com/the-sixt-india-blog/mocking-with-mockery-in-golang-949794372e99
●https://www.kelche.co/blog/go/golang-benchmarking/
●https://go.dev/doc/tutorial/fuzz
●https://go.dev/doc/security/fuzz/
●https://go.dev/blog/examples
●https://medium.com/insiderengineering/integration-test-in-golang-899412b7e1bf
●https://golang.testcontainers.org/
Resources

Thanks
QR code and Link to the repository
t.ly/dGA8f