testify icon indicating copy to clipboard operation
testify copied to clipboard

Much Simpler Testify

Open tylerstillwater opened this issue 12 years ago • 22 comments

Greetings, everyone!

I have been thinking for a while about creating a much simpler version of testify.

My goals would be to:

  • Make the API much smaller
  • Make the output more compact - probably a single line like the standard testing framework
  • ~~Use a require approach for all functions - abort if a test fails~~
  • Maintain the assert approach, with a require method that allows for a one-off require and fail. This will avoid deep nesting of if statements.
    • is.Equal(a,b).Require() or similar
  • Must manually output failure messages in the style of the testing framework, then call t.FailNow()
    • This will allow us to avoid using \r and \b which is incompatible with various output consoles.
    • Basically we need to duplicate the decorate function to generate our own output.
  • Remain completely dependent on the Go testing framework - testify should be as easy (or easier) to use as it is now... no fanciness.
  • Testify must take a testing.TB to enable testify usage in benchmark tests.

The reason this has been on my mind is because I usually use a very small subset of the available API in the current testify, and I feel that it may be getting a bit bloated. In addition, I feel that testify may be adding some overhead to the testing process, thus increasing the amount of time it takes to run tests. I really should get some data to back that up. In addition, testify was written a long time ago, when we were still young gophers. I feel it could be much cleaner. :)

I am still not sure this is something I will move forward with. I am hoping to get some feedback from the community to see what you think. Most likely this will live in its own subfolder as a separate package so the current version of testify doesn't just break for people currently using it.

I'd appreciate some feedback from all of you who use testify, if you would be so kind:

  • What functionality do you find yourself using the most?
  • What functionality could you simply not live without?
  • What is missing that you feel should be added?
  • What is in testify that you feel should be removed?
  • What is in testify that you feel could be improved?
  • Anything else?

Thanks in advance, everyone!

tylerstillwater avatar Sep 10 '14 02:09 tylerstillwater

Everything should follow the require pattern. The package or variable could be called is:

is := is.New(t)
is.Equal(a, b)
is.True(a)
  • Arguments should just append to the output as per log.Println, and have *f versions of methods for formatting (following fmt.Print* patterns)

matryer avatar Sep 10 '14 03:09 matryer

I wonder if there would be a way we could make it easier/simpler/whatever to write table driven tests.

tylerstillwater avatar Sep 10 '14 04:09 tylerstillwater

Following up on side-channel communication from slack, regarding "Use a require approach for all functions - abort if a test fails". With table driven tests the require pattern means you will only see the first row that fails. Seeing all possible rows that could fail is important because:

  • When fixing a test it is important to know if changes are causing breakages elsewhere
  • Seeing all the rows that failed after a change makes it easy to see a pattern that indicates the root cause (ie, only odd numbers are failing)

As to simplicity for table driven tests, I really like the existing workflow in Go with Testify. Typically I do something like this, and I cannot imagine something more elegant:

var sampleTests = []struct { name string, value bool }{
  { "Example pass", true },
  { "Example fail", false },
}
func TestSample(t *testing.T) {
  for _, s := range sampleTests {  assert.True(t, s.value, s.name)  }
}

I think if you want to have a single "package" for testing, instead of the current require/assert split you cannot prefer one functionality over the other. Quite often I will require something (ie, NewObject), followed by several asserts. The current language reads well (require this, assert that), but I understand it's messy maintaining both packages with 1 to 1 functionality. We talked about the option to set the behaviour of the test framework, but that can be ugly if you have to switch between the two (I only have to do that occasionally, I'm not sure how common a use case this is).

That said, this is what I'm currently thinking:

  • There is a default behaviour, is.FailOnError = false. This can be set by the user.
  • Functions return a subtyped version of bool on which the method Require is implemented. Now I can use is.Equal(a, b).Require() for a one off requirement.

matthias-stone avatar Sep 10 '14 07:09 matthias-stone

@matthias-stone I like your second idea, but with require being the default functionality, it would need to be inverted to something like is.Equal(a,b).Assert() which is odd.

Perhaps something like this, for those times you want assert functionality:

is := is.New(t)
a := is.Assert() // return a copy of `is` that doesn't fail on error

a.Equal(a,b) // continue if failed
is.Equal(c,d) // abort if failed
is.Assert().Equal(e,f) // continue if failed

I like this approach because you can either create a separate object for doing lots of asserts, or you can easily do a one-off assert. There's no toggling of a variable to control the behavior, making mistakes less likely and reducing complexity.

tylerstillwater avatar Sep 10 '14 14:09 tylerstillwater

I use suites all over the place, but if you wanted to drop support for suites, I think I could take that over in a separate package, as an extension to testify.

Either way, I like the changes proposed thus far. Suites (regardless of whether they stay in testify or move to an extension) could embed the proposed require type instead of Assertions to keep testify consistent.

nelsam avatar Sep 10 '14 15:09 nelsam

A new requirement:

  • Must manually output failure messages in the style of the testing framework, then call t.FailNow()
    • This will allow us to avoid using \r and \b which is incompatible with various output consoles.
    • Basically we need to duplicate the decorate function to generate our own output.

tylerstillwater avatar Sep 10 '14 15:09 tylerstillwater

New requirement:

  • Testify must take a testing.TB to enable testify usage in benchmark tests.

tylerstillwater avatar Sep 10 '14 15:09 tylerstillwater

@nelsam I've never used suites, but apparently they are quite popular. I think they would best be refactored into an extension package of the new testify. I'd love if you could make that your pet project once this moves forward.

tylerstillwater avatar Sep 10 '14 15:09 tylerstillwater

@tylerb I find them very useful for certain situations which seem to come up a lot in the code I write. That said, it may only be coming up because I don't know what I'm doing.

Examples:

  1. Setting up and tearing down database connections before and after all tests are run.
  2. Preparing data before each test and wiping it after each test.
  3. Running all tests multiple times with different settings. This is particularly useful for my query library simply to ensure that all code works the same way for all supported database engines. I could set up environment variables to define connection settings (as gorp does) and then use a shell script to run tests for all officially supported engines, but I prefer it this way.

When this starts moving forward, let me know, and we'll figure out a new place for suites to live.

nelsam avatar Sep 10 '14 18:09 nelsam

Very nice. Definitely needs a place in the new version. I'll let you know. Please do feel free to chime in any ideas or thoughts on this thread, of course.

tylerstillwater avatar Sep 10 '14 18:09 tylerstillwater

@tylerb could you please elaborate on why you think require should be the default behavior, and not assert? I found myself only needing require once in a while, e.g. when creating new objects, and using assert most of the time. To that extent, I really like is.Equal(a, b).Require() API.

Otherwise, everything on this thread looks good.

vkryukov avatar Sep 13 '14 19:09 vkryukov

I also hope that when you say that you want to minimize the API, you really mean assert/require split, and not reducing the number of different kinds of tests, e.g. Contains, Regexp, InDelta etc.

vkryukov avatar Sep 13 '14 19:09 vkryukov

@victorkryukov I seem to get buried pretty deeply in nested if conditions. It seems unnecessary given the tests in the if condition cannot be run if the parent test fails. However, having thought about it further, the if conditions are far fewer than the tests they contain. is.Equal(a, b).Require() would allow a quick one-off require instead of an if condition, then the assertions following could all run at the same time.

I think that is a better approach. Thoughts, everyone?

tylerstillwater avatar Sep 15 '14 01:09 tylerstillwater

@victorkryukov regarding reducing the number of tests: that isn't necessarily the goal, no, but I want to make sure everything that is included is well thought out and as elegant as it can be. I'm sure the rewrite will start with the basics and we can expand it from there as warranted.

tylerstillwater avatar Sep 15 '14 01:09 tylerstillwater

@tylerb any updates/progress on this? I like the proposed changes with .Require() for getting rid of those nested ifs.

brianchung808 avatar Nov 18 '14 21:11 brianchung808

  • What is missing that you feel should be added?

I have this one minor inconvenience that I keep running into: testing function execution. In other words, would love to see some kind of function spy testing mechanism. If anyone's ever used sinon.js, it's similar. Also, it's impossible to test for function equality in Go, so if you model your code to use lots of first-class functions you'll run into troubles when writing tests for them.

Hypothetical Example:

func (notif *Notication) SendPayload(payload *Payload) {
        notification.sender(payload)
}

Now to test that the function actually uses the specified sender we'd need some way to either test for function equality or some form of spies, usually by manually writing out a function that flips a local boolean flag:

func TestSendPayload_CallsSender(t *testing.T) {
    called := false
    func spy(payload *Payload) { called = true }

    n := notification.New()
    n.sender = spy
    n.SendPayload(&Payload{})
    assert.True(t, called)
}

Now the alternative is to make notification.sender have an interface then create implementations for all these small functions. I have no concrete suggestion at the moment but I think this could be achieved in one form or another.

Maybe I should file a separate issue? Not sure if this counts as making testify simpler but it will surely simplify quite several places in the various Go projects I'm writing.

EDIT: Just realize that this touch into the realm of what the mock package should cover. But still, don't really want to create a new type every time I need to test for simple function execution.

chakrit avatar Nov 28 '14 05:11 chakrit

@brianchung808 I've just pushed a new branch with the very basic beginnings of the rework. I haven't had any time to work on it in ages and that is likely to continue. Real life is getting in the way.

If anyone wants to take my humble beginnings and run with it, please feel free to do so. :)

tylerstillwater avatar Nov 28 '14 18:11 tylerstillwater

… or check out github.com/metabition/is http://github.com/metabition/is - that’s simpler :D

On Nov 28, 2014, at 10:56 AM, Tyler [email protected] wrote:

@brianchung808 https://github.com/brianchung808 I've just pushed a new branch with the very basic beginnings of the rework. I haven't had any time to work on it in ages and that is likely to continue. Real life is getting in the way.

If anyone wants to take my humble beginnings and run with it, please feel free to do so. :)

— Reply to this email directly or view it on GitHub https://github.com/stretchr/testify/issues/84#issuecomment-64921335.

matryer avatar Nov 28 '14 18:11 matryer

Yes, definitely!

tylerstillwater avatar Nov 28 '14 18:11 tylerstillwater

@matryer :+1:

brianchung808 avatar Dec 01 '14 06:12 brianchung808