Smarty

Your Convey needs more Focus

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...
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
Black Friday ready: Real-time ecommerce address verification
Arrow Icon
Don’t panic. We’re not telling you to set up your Christmas lights in July or tune into your favorite holiday station. We’re not crazy here (mostly). But also, do your thang. What we ARE saying is that if you’re in ecommerce, you know just as well as Smarty does that you have to start prepping for those big holidays months in advance. Today is as good as any to see how setting up the right real-time address capabilities, like suggesting verified customer addresses as they type, can get your business in the black on Black Friday.
Address Autocomplete API JavaScript SDK tutorial
Arrow Icon
Webinar recap: Ready to take your web forms from "meh" to "wow"?In our recent webinar, we gave tips and tricks and demonstrated live coding magic. The essentials of Smarty's address autocomplete APIJavaScript SDK magic made easyIntegrating Smarty’s Address Autocomplete API using our JavaScript SDK is a breeze. Smarty also has SDKs for . NET, Android, Go, iOS, Java, PHP, Python, Ruby, and Rust, making setting up client credentials, building functions, and handling address lookups look like child’s play.
How Smarty™ sidestepped the global IT outage with 100% uptime
Arrow Icon
A recent defective update from CrowdStrike, a leading cybersecurity provider, caused a global outage affecting millions of Windows systems. Oops. The faulty update to CrowdStrike's Falcon software, intended for endpoint detection and response, led to the infamous "blue screen of death" (BSOD) and rendered many systems non-bootable. The widespread disruption primarily impacted large organizations reliant on CrowdStrike's services, sparing home PCs and systems running on Mac and Linux. Like the rest of the world, Smarty employees were also impacted.