This article will show how to “test drive” the prime factors kata in Go with Testify. Testing in Go is built right in, which is great. It’s a very bare-bones implementation though so adding Testify to the mix makes it a lot easier to write your tests.

For an explanation of Test-Driven Development (TDD) itself, see this article.

Problem: We want to factor a number into it’s base primes. For example 9 can be factored into 3x3 and 20 can be factored into 2x2x5.

Let’s create a module and install Testify

```
go mod init prime_factors
go get github.com/stretchr/testify
go get github.com/stretchr/testify/assert@v1.9.0
```

The first stage in TDD is **red** where we create a failing test. It’s important to remember that we don’t want to write any production code until that’s the only way we can make a test pass.

### First test: factor 2

For our first test, we’ll attempt to factor 2, which should result in an array containing only two.

In prime_factors_test.go, we have:

```
package prime_factors
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Test_2(t *testing.T) {
assert.Equal(t, factor(2), []int{2})
}
```

Initially, this won’t even compile so it isn’t failing for the right reason. When we run `go test`

, we see

```
# primefactors [primefactors.test]
./prime_factors_test.go:9:21: undefined: factor
FAIL primefactors [build failed]
```

We write enough code to allow it to compile and run.

```
package prime_factors
func factor(number int) []int {
return []int{}
}
```

We verify that the test is still failing, now for the right reason.

```
Not equal:
expected: []int{}
actual : []int{2}
```

Next we move to **green**, where we need to make the test pass. We’re not looking to make the code beautiful or performant or reusable at this point. We’re looking to make the test pass in the easiest possible way.

```
func factor(number int) []int {
return []int{2}
}
```

We know that hard coding the 2 at this point is silly and that it’s going to cause problems later but we don’t care. The TDD process will get us there at the right time. For now, we only care about the one case that we’ve written a test for.

Now we come to the third stage in the process, **refactor**. We look at the code and the tests and we look to see if there’s anything we might want to improve. There usually isn’t for the first test but there certainly will be things to improve once we have a couple of tests and so we always want to consider this.

Check it in. It’s certainly not complete but what we do have is a tiny slice of working, tested, code.

### Second test: factor 3

We already recognized that hard coding the 2 was bad so let’s create another test that forces us to fix that. We’re back in **red**

We’re adding a second test here, not replacing the first. We are building up a suite of these tests.

```
func Test_3(t *testing.T) {
assert.Equal(t, factor(3), []int{3})
}
```

We run the test and it fails for the right reason.

```
Not equal:
expected: []int{2}
actual : []int{3}
```

We satisfied red and now move on to **green**. Change the production code to make the test pass. The simplest thing that could work.

```
func factor(number int) []int {
return []int{number}
}
```

Now both tests are passing so we consider the **refactor** step. Is there any duplication? Anything we might want to improve at this point? I’m thinking no, so we’ll move on.

Check it in.

### Third test: factor 4 (two times a prime)

**red**: The test for 4 is straight forward. We should get back two and two.

```
func Test_4(t *testing.T) {
assert.Equal(t, factor(4), []int{2, 2})
}
```

**green**: The implementation is more complex here because for the first time, we have a chance to actually calculate something instead of just hard coding.

I notice that any even number can be divided by 2 so check if it’s even and then split into 2 and whatever is left.

```
func factor(number int) []int {
result := make([]int, 0)
if number % 2 == 0 {
result = append(result, 2)
number = number / 2
}
result = append(result, number)
return result
}
```

When the tests are run again, we discover that while the test for 4 (the immediate concern) is passing, one of our earlier tests is now failing. Specifically the test for 2 which is now returning [2, 1]

We’re still in the **green** phase so we want the simplest solution that gets us working again. We’ll add a special case for 2.

```
func factor(number int) []int {
result := make([]int, 0)
if number % 2 == 0 && number != 2 {
result = append(result, 2)
number = number / 2
}
result = append(result, number)
return result
}
```

All tests are passing so we move on to **refactor**. I’m really not liking all the hard coded 2’s but I’m not sure how I want to handle that yet so I’ll defer fixing it. It’s likely now that we’ll work for two times any prime but we may not work for powers of two times a prime. Let’s test-drive that.

Check it in.

### Fourth test: factor 8 (power of two times a prime)

**red**: Create a test for 8 and watch it fail.

```
func Test_8(t *testing.T) {
assert.Equal(t, factor(8), []int{2, 2, 2})
}
```

**green**: We really just want to execute that if condition multiple times so what if we change the *if* to a *for*?

```
func factor(number int) []int {
result := make([]int, 0)
for number % 2 == 0 && number != 2 {
result = append(result, 2)
number = number / 2
}
result = append(result, number)
return result
}
```

Now it looks like we’ve handled all powers of two times a prime.

**refactor**: Back to the refactor step, and I’m increasingly unhappy with all the hard coded 2’s so let’s deal with that now.

```
func factor(number int) []int {
result := make([]int, 0)
divisor := 2
for number % divisor == 0 && number != divisor {
result = append(result, divisor)
number = number / divisor
}
result = append(result, number)
return result
}
```

All we’ve really done is make a constant so 2 is still hard coded. It’s only in one place now, however, which makes the code feel much cleaner to me.

All tests continue to pass so we move on.

Check it in.

### Fifth test: factor 9 (power of two times a prime)

**red**: We know that powers of two are all working and it feels pretty obvious that it’s going to break when we attempt multiple of 3 so let’s start there. Next test is for 3x3

```
func Test_9(t *testing.T) {
assert.Equal(t, factor(9), []int{3, 3})
}
```

Test fails, and for the right reason.

```
Not equal:
expected: []int{9}
actual : []int{3, 3}
```

**green**: For the case of 9, we really want to execute the whole *while* for both 2 and 3 so what if we did a loop within a loop?

```
func factor(number int) []int {
result := make([]int, 0)
for divisor := 2; divisor <= 3; divisor++ {
for number % divisor == 0 && number != divisor {
result = append(result, divisor)
number = number / divisor
}
}
result = append(result, number)
return result
}
```

All tests continue to pass.

**refactor**: The code and the tests are both looking pretty clean so nothing I want to change. We still always need to stop and reflect at this point though.

Check it in.

### Sixth test: factor 25

If we think our way through all the numbers as we go up, we can see that we’ve handled every power of two times and prime and every power of three times a prime but we don’t handle fives.

**red**: So let’s test for 25.

```
func Test_25(t *testing.T) {
assert.Equal(t, factor(25), []int{5 ,5})
}
```

**green**: The simplest possible thing to make the test pass would be to increase the range to 5.

```
func factor(number int) []int {
result := make([]int, 0)
for divisor := 2; divisor <= 5; divisor++ {
for number % divisor == 0 && number != divisor {
result = append(result, divisor)
number = number / divisor
}
}
result = append(result, number)
return result
}
```

All tests continue to pass. Now I wonder, if we were able to make it work by only changing the range, could I make the range large enough right now to solve for every possible case? We know the divisor can’t be larger than the original number passed in so let’s set the high end of the range to that.

```
func factor(number int) []int {
result := make([]int, 0)
for divisor := 2; divisor <= number; divisor++ {
for number % divisor == 0 && number != divisor {
result = append(result, divisor)
number = number / divisor
}
}
result = append(result, number)
return result
}
```

Everything continues to pass.

At this point, I think we’ve handled every possible case. We might want to spot-check some larger numbers to see if we can uncover something that we didn’t handle, but that’s more **exploratory testing** and not **test-driven development** so we’ll stop here.

Check it in.

### Recap

We took one step at a time to solve the problem. We created one tiny test and watched it fail (**red**), we did the simplest thing to make it pass (**green**), and we looked for opportunities to make it better (**refactor**). Then we did it again, and again, until we’d finished the feature.