Bifalgorithm post

"We usually write the code first and add tests later, if we add them at all. But in test-driven development, you don't get to write production code until you have a test that fails. And you write just enough of that test to fail, and then you stop. Then you write just enough production code to pass that test, and no more. That's a lot of stopping, but it's why your tests always stay one step ahead of your code. Say the job is to work out whether someone is allowed to edit a post. So you start with the test and it calls a function even if nobody has written that yet. It doesn't even compile and that already counts as failing. So you stop writing the test and write the smallest thing that makes it pass. And that's enough. It says yes to everyone, which looks like cheating, until the second test arrives. That one asks about someone who is not the author and yes is the wrong answer. So again, you write just enough production code to pass it. Every line of production code exists because a test asked for it. So your code is always covered by tests even before it's written. Follow for more clean code principles coming next."

"We usually write the code first and add tests later, if we add them at all. But in test-driven development, you don't get to write production code until you h…

Ideas in this flowchart

  • Test-Driven Development
  • Traditional Approach
  • Code first, tests later
  • Test-Driven Approach
  • Write test before code
  • Failing test prerequisite
  • Stop after writing failing test
  • Write minimal code to pass test
  • Tests lead code
  • Example Scenario
  • Edit post permission
  • Initial test call
  • Test fails at compile
  • Write minimal passing code
  • Second test scenario
  • Non-author edit attempt
  • Code evolves with tests
  • Code coverage by tests
  • Follow for more principles
Feed/

"We usually write the code first and add tests later, if we add them at all. But in test-driven development, you don't get to write production code until you have a test that fails. And you write just enough of that test to fail, and then you stop. Then you write just enough production code to pass that test, and no more. That's a lot of stopping, but it's why your tests always stay one step ahead of your code. Say the job is to work out whether someone is allowed to edit a post. So you start with the test and it calls a function even if nobody has written that yet. It doesn't even compile and that already counts as failing. So you stop writing the test and write the smallest thing that makes it pass. And that's enough. It says yes to everyone, which looks like cheating, until the second test arrives. That one asks about someone who is not the author and yes is the wrong answer. So again, you write just enough production code to pass it. Every line of production code exists because a test asked for it. So your code is always covered by tests even before it's written. Follow for more clean code principles coming next."

"Test-Driven Development"

👍
0
0 comments0 shares

Sign in to view and post comments.