Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Manage test dependencies with Go

September 19, 2017
Konrad Kleine
Related topics:
Developer toolsGo
Related products:
Red Hat OpenShift Container Platform

    Introduction

    I'm working on the upstream fabric8-wit project of openshift.io. In this Go project, we embrace testing as best as we can in order to deliver a stable component. Testing acts as our safety net to allow for fast-paced feature development. This blog post is about our recent change in our testing strategy. It is not as boring as it might sound at first. ;-)

    Problem description

    We've changed out the data-model quite a lot and it took us a while to realize what this means to our tests. Consider this simple data-model that is pretty close to reality:

    A work item (e.g. a Bug) is part of a space (e.g. your project) and has a work item type (e.g. the template for a Bug) and an author (an Identity object).

    We have repositories for each of those entities that can do basic CRUD operations. Alongside those repositories, we have tests to check that those CRUD operations do work and fail as expected.

    But it was only until recently that, we had to create all the dependencies of an entity under test ourselves. For example, when we wanted to test CRUD operations on a work item, we had to manually create a space, a work item type, and an identity.

    The downside is threefold:

    1. You're only interested in the work item but you have to deal with things that are of no interest to you (space, work item type, and identity).
    2. You have to know dependencies of each individual entity.
    3. Consider you modify one dependency in the chain. Then you might have to touch all the places where it is being used.

    Especially the last point is very annoying.

    Vision

    In order to address the above-named issues, we were looking for a new way to write our tests. You should be able to test a work item repository CRUD-method without the need to create all dependencies. Just say that you need 10 work items and that's it. The rest is taken care of for you automatically.

    We envisioned something like this:

    func TestListWorkItems(t *testing.T) {
        // given 10 work items
        fxt := NewTestFixture(t, WorkItems(10))
        // test that you can list all 10 of them
        // ...
    }

    Think of the fxt variable to have a structure similar to this one:

    type TestFixture struct {
      Identities    []*account.Identity
      Spaces        []*space.Space
      WorkItemTypes []*workitem.WorkItemType
      WorkItems     []*workitem.WorkItem
      // more entities
    }

    So, after the call to NewTestFixture() you can use fxt.WorkItems[idx] with idx being in the range from 0 to 9. Also, you can expect the other slices to be filled according to the dependencies that were triggered by the call to WorkItems(10). So for example, fxt.Identities fxt.Spacesand fxt.WorkItemTypes contain 1 item each. That is the least number of entities that need to exist in order to create one work item.

    Additional requirements

    Influence creation

    Soon we realized that we might want to influence how each work item is created. For example, let's say, we want 10 work items each belonging to a different user. That means, we need 10 identity objects as well:

    NewTestFixture(t, Identities(10), WorkItems(10))

    Just by coincidence, the numbers of both entities match. Yet, the identity objects won't automatically be used as the author in the 10 work items. The system shall always use the least dependency object, in this case fxt.Identities[0]. Here's how to address this:

    NewTestFixture(t, Identities(10), WorkItems(10, func(fxt *TestFixture, idx int){
       fxt.WorkItems[idx].AuthorID = fxt.Identities[idx].ID
    })

    Notice, the callback gives you access to a fully initialized work item object at index idx. At this point, the identity objects are already created and can be accessed safely from the test fixture object fxt that is passed into the function. It is only after this anonymous function returns that the system creates the fxt.WorkItems[idx] object.

    Request multiple objects at once

    In the above example, you've already seen this in action. The function NewTestFixture()obviously can handle an arbitrary number of so-called recipe functions (e.g. Identities()and WorkItems()). The test fixture is heavily documented in order to be easily consumable by others. Each recipe tells you what dependencies it has and allows you to manipulate the creation using a special form of customization function.

    Flexibility within limits

    We use Go's strong type system to ensure compile-time checks on the customization functions. Each recipe only accepts a dedicated type of function as a specialization. That allows us to provide pre-defined functions like TopologyTree() that can only be used with the recipe function. WorkItemLinkTypes() This avoids misuse of those pre-defined functions.

    Order of recipe functions

    The order of recipe functions must not matter. Hence, these two test fixtures are the same:

    NewTestFixture(t, Identities(10), WorkItems(10))
    
    NewTestFixture(t, WorkItems(10), Identities(10))

    The test fixture must create the entities in the right order.

    Benefits

    We have introduced this new test fixture in some places already. Most of the time, we cut the test code in half. On its own, this is quite an achievement in maintainability. When it comes to changing our data-model again, there's only one place to go to. That place is inside the test fixture package.


    Whether you are new to Containers or have experience, downloading this cheat sheet can assist you when encountering tasks you haven’t done lately.

    Last updated: October 31, 2023

    Recent Posts

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    • Every layer counts: Defense in depth for AI agents with Red Hat AI

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.