How NOT to Write a Test in Go - Amir Malka

Don't write tests in Go without learning from common pitfalls and mistakes. Discover how to avoid errors, improve your testing strategies, and write reliable code with Go's testing package and more.

Key takeaways
  • Test categorization: Tests should be categorized based on their purpose, such as unit tests, integration tests, and system tests.
  • Table-driven tests: Table-driven tests are useful for testing functions with multiple inputs and expected outputs.
  • Mocking: Mocking is a technique used to isolate dependencies in a unit test.
  • Cleanup: Cleanup is a mechanism used to clean up resources used in a test.
  • Benchmarks: Benchmarks help measure the performance of code.
  • Code coverage: Code coverage is a measure of how much of the code is executed during a test.
  • Test automation: Test automation is a technique used to run tests automatically.
  • Go’s testing package: Go’s testing package provides built-in support for test automation, including support for table-driven tests, mocking, and cleanup.
  • Mocking external dependencies: Mocking external dependencies is important for testing to avoid issues with external services.
  • Ran testing: Ran testing is a technique used to run tests in parallel.
  • Test naming conventions: Test naming conventions should follow a standard format, such as test_cpp_hello_world.
  • Code organization: Code organization is important for testing, with separate packages and files for tests and code.
  • Test ordering: Test ordering is important to ensure that tests are run in a specific order.
  • Test data: Test data is important to ensure that tests are run with the same data.
  • Test results: Test results should be analyzed to identify issues with the code.
  • Code reviews: Code reviews are important for ensuring that code is correct and maintainable.
  • Testing for data race: Testing for data race is important to ensure that code is thread-safe.
  • Testing for functional correctness: Testing for functional correctness is important to ensure that code does what it is supposed to do.
  • Testing for performance: Testing for performance is important to ensure that code performs well.
  • Using the Go testing package: The Go testing package provides built-in support for testing, including support for table-driven tests, mocking, and cleanup.
  • Using a testing runtime: A testing runtime is a mechanism used to run tests in a sandboxed environment.
  • Using a testing framework: A testing framework is a mechanism used to run tests in a organized way.
  • Code organization: Code organization is important for testing, with separate packages and files for tests and code.
  • Test automation: Test automation is a technique used to run tests automatically.
  • Benchmarking: Benchmarking is a technique used to measure the performance of code.
  • Code coverage: Code coverage is a measure of how much of the code is executed during a test.
  • Test ordering: Test ordering is important to ensure that tests are run in a specific order.
  • Test data: Test data is important to ensure that tests are run with the same data.
  • Test results: Test results should be analyzed to identify issues with the code.
  • Code reviews: Code reviews are important for ensuring that code is correct and maintainable.
  • Testing for data race: Testing for data race is important to ensure that code is thread-safe.
  • Testing for functional correctness: Testing for functional correctness is important to ensure that code does what it is supposed to do.
  • Testing for performance: Testing for performance is important to ensure that code performs well.