Your Convey needs more FocusOne of the great benefits of TDD/BDD is that you usually don't have to spend much, if any time at all in a debugger...
February 7, 2014
Tags

One of the great benefits of TDD/BDD is that you usually don't have to spend much, if any time at all in a debugger. To enter a debugger is to admit a loss of control over the system under test. Even so, there are times when you do need to debug something, even if you're maintaining the discipline.

Lately, most of my coding is in GoLang. Coming from using an IDE almost exclusively to write Python (using PyCharm) and C# (using VS and ReSharper), and knowing how great the visual debugging tools are it's hard to fathom using a console-based debugger for GoLang code. Yes, I know about godbg, which is an amazing tool, but I would rather stay in my test runner of choice, which is GoConvey.

Up until now I would do something like this to achieve debugging:

  • Add log.Println statements where the bugs are
  • Use SkipConvey to limit execution to the test in question (this can get tedious if the test suite is large)
  • Run the tests and observe the output

Or...

  • Add the logging statements
  • Use an init function in the test code to substitute a nil writer to the logger
  • Substitute stdout in the Convey scope that executes the code with logging
  • run the tests and observe the output

Yuck.

Now, there's a better way. It still means putting in some logging, which may or may not be temporary. But, all you have to do now is something like this:

FocusConvey("Subject: Integer incrementation and decrementation", t, func() {
	var x int

	FocusConvey("Given a starting integer value", func() {
		x = 42

		FocusConvey("When incremented", func() {
			x++

			Convey("The value should be greater by one", func() {
				So(x, ShouldEqual, 43)
			})
			FocusConvey("The value should NOT be what it used to be", func() {
				So(x, ShouldNotEqual, 42)
			})
		})
		Convey("When decremented", func() {
			x--

			Convey("The value should be lesser by one", func() {
				So(x, ShouldEqual, 41)
			})
			Convey("The value should NOT be what it used to be", func() {
				So(x, ShouldNotEqual, 42)
			})
		})
		Reset(func() {
			x = 0
		})
	})
})

In this code, only those scopes declared with FocusConvey will be executed, the reset will be ignored. Much easier to toggle for debugging purposes.

Of course, you shouldn't leave the test suite with a bunch of FocusConvey calls lying around. After you fix your code get rid of the Focus and just keep Convey.

Happy debugging!

Subscribe to our blog!
Learn more about RSS feeds here.
rss feed iconSubscribe Now
Read our recent posts
Navigating the Future: Geocoding & Address Data Trends in the Cloud
Arrow Icon
In a recent webinar, Berk Charlton, Chief Product Officer at Smarty, provided an in-depth look at our industry-leading address intelligence suite designed to provide highly accurate geocoding and address validation. Here are the main points Berk covered during the session:Comprehensive Product Suite: Smarty offers a wide range of products, including address validation, rooftop geocoding, global address auto-complete, and address data enrichment. These tools are designed to handle various aspects of address management and enhancement in industries from Insurance to Healthcare to Telecom and more.
Unlocking Ecommerce Success: Fabletics' Journey with Address Autocomplete
Arrow Icon
In the growing world of ecommerce, providing a seamless checkout experience is key for customer satisfaction and conversion rates. Fabletics, a leading activewear brand, embarked on a conversion-improving journey with Smarty to enhance their checkout process. We recently hosted a webinar filled with insights from Mel Cummings, Vice President of Product Management and UX at Fabletics. Unlocking Ecommerce SuccessConsider this common scenario: your customer is navigating through an online checkout process, only to be thwarted by an error, often related to address entry.
Avoid Awkward Moments with Smarty's Accurate Data
Arrow Icon
March 18 marks a bit of an awkward holiday: Awkward Moments Day. It's a day that celebrates those times that make you cringe, moments that make you face-palm, and stories that wake you up at 4 in the morning thinking, "Ugh! Why did I do that?" Although these moments can be uncomfortable, it's helpful to remember that everyone has them, and if they say they don't … We know they’re lying.  It's also helpful to remember that these embarrassing stories can help us learn and do better the next time around.
Ready to get started?