Just like a knight sharpens their sword before battle, we want to make sure our code is ready for action!
Writing Tests in Zig
Writing Tests is like setting up a training ground for your code. You want to make sure it’s prepared for any challenges it might face.
test "Addition Test" {
const sum = add(2, 3);
testing.expect(sum == 5);
}
Imagine you’re a coach training a team. What kind of exercises would you give them to prepare for a big game?
Running Tests
Running Tests is like watching your team in action. You want to see how well they perform under different conditions.
zig test my_code.zig
If you were a coach, how would you evaluate your team’s performance after a practice match?
Test Assertions and Cases
Assertions are like referees in a game. They make sure everything is fair and square.
testing.expect(myFunction() == expectedValue);
Test Cases are like different scenarios in a training session. They help us cover all possible situations.
test "Scenario 1" {
// Test code for scenario 1
}
test "Scenario 2" {
// Test code for scenario 2
}
If you were designing a series of challenges for your team, what kind of scenarios would you include to test their skills?
Now, let’s try writing tests for our code. What kind of challenges or scenarios would you like to prepare for?
Need further assistance, paid dedicated one-to-one tutor, need assignments done or have a paid project in Zig?
Leave a Reply