TDD & Sublime Workflow

This morning I spent some time streamlining my test driven development workflow. After reading and watching many tutorials on Web Development, TDD and Laravel there have been a couple of things that I’ve seen glimpses of and thought to myself “how did you get that so quickly?” or “where did that come from?”. This morning I take a moment to work it out and implement the methods myself.

Sublime Text

I’ve switched to Sublime Text. Previously I had picked Atom as an IDE for Web Development. The main reason for this was because it was free with good reviews and offered a lot of the features I believed I would need. After spending time learning web development and watching many videos on the subject I couldn’t help but notice that the authors were using Sublime Text. One of the main reasons for swapping was that when I’m following along with the tutorials I wanted to be doing exactly what the author is doing without translating the process into the way that Atom does it. Also when watching the tutorials I’ve been picking up on shortcuts and tricks to help with the workflow of creating a web application. These shortcuts and code snippets may be possible in a similar fashion within Atom but I already had the material of how to do them for Sublime Text and I would be wasting my time learning if Atom has a similar feature and then working out where I can find the plugin for Atom to do it. I firstly needed to streamline my learning by using the same IDE as the teachers I was learning from. I could then streamline my coding, to be a fast more production app developer.

New File with Name

One of the first things I saw when watching tutorials was the author creating a new file and be presented with the option to give the file a name before it was made.

Sublime Text by default doesn’t have this option although looks likes it would on first interaction due to the fact that creating a new folder presents a very similar view.

It turns out I needed a 3rd party package called Advanced New File. Now I can do exactly the same as the authors of the tutorials I’m watching with the keyboard shortcut [alt] + [cmd] + [n].

Test Snippets

I can touch type relatively well but sometimes when watching the videos all of sudden there is lines of code that have appeared out of nowhere and you wonder “where did all of that come from?”. Snippets are the answer. In Sublime Text you can have many snippets of text which can be triggered to be written automatically by a keyboard shortcut. Adam Wathan’s blog on Optimizing Your PHPUnit Workflow in Sublime Text provides two very useful snippets for boilerplate code for test case classes and test methods. Adam and David Hemphill also created a Sublime Text package called Sublime PHPUnit designed to streamline unit tests and automate the running of them from within Sublime.

  • [alt] + [cmd] + [t] to run a single test.
  • [alt] + [t] to run all tests within the file.

Inserting use statements

A few times when working through a video tutorial learning Test Driven Development I would end up with a test error - “Error: Class ‘Carbon’ not found. This is an easy error to diagnose but when starting out I was pulling my hair out trying to work out where I had gone wrong. I had been following the tutorial to the letter. Low and behold the author had used a keyboard shortcut to insert the “use” class statement at the top of the file. This functionality is brought in with another 3rd party package called PHP Companion

Now I just bring my cursor over a class name, hit the F5 key and it will insert the use statement at the top of the file automatically for me.

Compete workflow

  1. Create a new file (say FeatureTest.php).
    • type [alt] + [cmd] + [n]
    • type FeatureTest.php
    • Hit ENTER
  2. Generate test case class.
    • Place the cursor in FeatureTest.php file
    • Type testcase
    • Hit TAB
  3. Generate a test method.
    • Place the cursor in FeatureTest.php file where you would like the method to sit
    • Type test
    • Hit TAB
  4. If a class is used for the test insert the use statement.
    • Hover mouse of the class name
    • Hit F5
  5. When the test has been written, run it.
    • [alt] + [cmd] + [t] or [alt] + [t]

SMART Goal

  • Specific - Apply the TDD & Sublime Workflow into my current development practise.
  • Measurable - Each new test file or test case that needs creating use the workflow. Tally how many times you have used it per day of development to mark your progress and promote muscle memory.
  • Attainable - Use the journal post to remind you of the keyboard shortcuts and stick post it notes around your work space to promoting the use of the workflow.
  • Relevant - Become more efficient when writing repetitive code.
  • Time-Based - By the end of the next study block.

References

  1. Sublime Text
  2. Atom
  3. Advanced New File
  4. Optimizing Your PHPUnit Workflow in Sublime Text
  5. Sublime PHPUnit
  6. PHP Companion