stackconf 2024 | Test like a ninja with Go by Ivan Presenti.pdf
NETWAYS
62 views
58 slides
Jul 26, 2024
Slide 1 of 58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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...
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’ll introduce you to the most popular libraries and packages used to test Go code.
Size: 3.31 MB
Language: en
Added: Jul 26, 2024
Slides: 58 pages
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