Tillitsdone
down Scroll to discover

Getting Started with Assertions in Testify

Learn how to use Testify's assertion package in Go to write more expressive and maintainable tests.

Discover common assertions and best practices for effective testing.
thumbnail

Getting Started with Assertions in Testify

Abstract digital waves representing software testing with flowing cyan and violet gradients creating a dynamic pattern of interconnected lines and nodes high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Testing is a crucial part of software development, and Go provides excellent tools for ensuring your code works as expected. While Go’s built-in testing package is powerful, Testify takes it to the next level by providing a rich set of assertions that make your tests more expressive and easier to write.

What is Testify?

Testify is a widely-used testing toolkit for Go that extends the standard testing package with additional functionality. One of its most valuable features is the assertions package, which provides a comprehensive set of assertion methods to validate your test results.

Geometric patterns forming a unified structure with bright green crystalline shapes interconnecting in a harmonious arrangement against a deep space background high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Getting Started with Assertions

To begin using Testify assertions, first install the package:

go get github.com/stretchr/testify

Let’s dive into a practical example. Imagine we have a simple calculator function:

func Add(a, b int) int {
return a + b
}

Here’s how we can test it using Testify:

func TestAdd(t *testing.T) {
assert := assert.New(t)
result := Add(2, 3)
assert.Equal(5, result, "2 + 3 should equal 5")
}

Common Assertions

Testify provides numerous assertion methods that cover most testing scenarios. Here are some of the most frequently used ones:

Equal and NotEqual

assert.Equal(t, expected, actual, "these should be equal")
assert.NotEqual(t, expected, actual, "these should not be equal")

True and False

assert.True(t, value, "this should be true")
assert.False(t, value, "this should be false")

Nil and NotNil

assert.Nil(t, object, "this should be nil")
assert.NotNil(t, object, "this should not be nil")

Organic flowing patterns resembling natural circuits with bright cyan and violet energy streams weaving through transparent layers creating a sense of depth and movement high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

Best Practices

  1. Use descriptive error messages
  2. Group related assertions together
  3. Test both positive and negative cases
  4. Keep tests focused and simple

Let’s see these practices in action with a more complex example:

func TestUserValidation(t *testing.T) {
assert := assert.New(t)
user := User{
Name: "John Doe",
Email: "john@example.com",
Age: 25,
}
// Group related assertions
assert.NotEmpty(user.Name, "Name should not be empty")
assert.Contains(user.Email, "@", "Email should contain @")
assert.Greater(user.Age, 18, "User should be an adult")
}

Conclusion

Testify’s assertions make writing tests in Go more intuitive and maintainable. By providing clear, expressive methods for validating your code’s behavior, it helps you create more robust applications with confidence.

Crystal formations in natural patterns with bright green and cyan colors blending harmoniously creating a serene and balanced composition with geometric elements high-quality ultra-realistic cinematic 8K UHD high resolution sharp and detail

icons/logo-tid.svg Latest Blogs
Discover our top articles, selected to support the growth of your business.
https://imgproxy-landing-page.tillitsdone.com/sig/rs:fit:1200:630/plain/https%3A%2F%2Fcms-r2.tillitsdone.com%2Fwp-content-prod%2Fuploads%2F2025%2F05%2FTill-its-done_SEO_R08_apr_1440x697.jpg@webp รู้จักกับ บริษัท Software House คืออะไร ทำอะไรบ้าง Software House คือศูนย์บริการที่ครบวงจรในการพัฒนาเทคโนโลยี ช่วยสนับสนุนธุรกิจในยุค 4.0 และสร้างโอกาสใหม่ ๆ ในตลาดการแข่งขันที่มีการเปลี่ยนแปลงอย่างรวดเร็ว https://imgproxy-landing-page.tillitsdone.com/sig/rs:fit:1200:630/plain/https%3A%2F%2Fcms-r2.tillitsdone.com%2Fwp-content-prod%2Fuploads%2F2025%2F05%2FTill-its-done_SEO_R07_apr_1440x697.jpg@webp Mobile App Developer คืออาชีพอะไร และมีความสำคัญอย่างไร Mobile App Developer มีบทบาทสำคัญในการขับเคลื่อนธุรกิจในยุคดิจิทัล โดยมุ่งพัฒนาประสบการณ์ผู้ใช้ และสนับสนุนการเติบโตขององค์กรในอนาคต https://imgproxy-landing-page.tillitsdone.com/sig/rs:fit:1200:630/plain/https%3A%2F%2Fcms-r2.tillitsdone.com%2Fwp-content-prod%2Fuploads%2F2025%2F05%2FTill-its-done_SEO_R06_apr_1440x697.jpg@webp React Native คืออะไร ทำความรู้จัก และเริ่มต้นสร้าง Project React Native คือ Framework ที่ช่วยให้นักพัฒนาสร้างแอปมือถือ โดยมีประสิทธิภาพใกล้เคียงกับ Native App ซึ่งลดเวลาและค่าใช้จ่ายในการพัฒนา แต่ทำได้ยังไงกันนะ https://imgproxy-landing-page.tillitsdone.com/sig/rs:fit:1200:630/plain/https%3A%2F%2Fcms-r2.tillitsdone.com%2Fwp-content-prod%2Fuploads%2F2025%2F05%2FTill-its-done_SEO_R02_apr_1440x697-1.jpg@webp Website Development คืออะไร สำคัญอย่างไร Website Development เป็นกระบวนการที่สำคัญในการสร้างเว็บไซต์ ซึ่งจะช่วยให้ธุรกิจของคุณเติบโตในตลาดออนไลน์ได้อย่างยั่งยืนและมีประสิทธิภาพ image_generation/Debug-TailwindCSS-with-DevTools-1732752708935-cdd0a53458db0224ae03d6d0b9599879.png Debug TailwindCSS Issues with Browser DevTools Learn practical techniques for debugging TailwindCSS using browser DevTools. Master the cascade, understand style overrides, and solve common responsive design issues efficiently. image_generation/Jest-Coverage-Reports-Guide-1732733982763-bc09ffcd377b2159e9e17e9d31cc1515.png Using Jest's Coverage Reports for Better Tests Learn how to leverage Jest's coverage reports to write more effective tests, understand coverage metrics, and set meaningful thresholds to maintain high-quality code in your projects.
icons/logo-tid.svg

พูดคุยกับซีอีโอ

พร้อมที่จะสร้างเว็บ/แอปของคุณให้มีชีวิตชีวาหรือเสริมทีมของคุณด้วยนักพัฒนาชาวไทยผู้เชี่ยวชาญหรือไม่?
ติดต่อเราวันนี้เพื่อหารือเกี่ยวกับความต้องการของคุณ แล้วมาสร้างโซลูชันที่ปรับแต่งเพื่อบรรลุเป้าหมายของคุณกัน เรายินดีช่วยเหลือทุกขั้นตอน!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
FacebookInstagramLinkedIn
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.