Share
Embed
TDD anti patterns - episode 3 - With Juan Pablo
Testing practices has increasing its adoption by developers, shifting left the test responsibilities
And increasing the quality of the code, besides that, continuous testing is an agile practice that impacts the software development life cycle.
In this talk we are going to focus on the following TDD anti-patterns: The nitpicker, The secret catcher, The dodger and The Loudmouth. This is the third out of 6 in the testing anti-patterns series.
Marabesi
February 10, 2022
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
103 slides
-
TDD - EP 3 codurance.com Testing anti-patterns - The nitpicker,
The secret catcher, The dodger, The Loudmouth -
Matheus Marabesi Hello there, you can call me Marabesi, But
my name is Matheus Marabesi, I work at Codurance as a Software Craftsperson. I enjoy talking about anything related to: testing, patterns and gamification. You can find me at @MatheusMarabesi or https://marabesi.com Codurance Crafting Code -
1. Intro - Recap 2. The nitpicker 3. The secret
catcher 4. The dodger 5. The Loudmouth 6. Wrapping up Crafting code Agenda -
The Liar The Giant The Mockery The Inspector Generous Leftovers
The Local Hero The Nitpicker The Secret Catcher The Dodger The Loudmouth Anti patterns The Greedy Catcher Excessive Setup The Sequencer Hidden Dependency The Enumerator The Stranger The Operating System Evangelist Success Against All Odds The Free Ride The One The Peeping Tom The Slow Poke James Carr - TDD Anti-Patterns -
The Liar 4 The Giant 5 The Mockery 1 The
Inspector 7 Generous Leftovers 5 The Local Hero 7 The Nitpicker The Secret Catcher The Dodger The Loudmouth Anti patterns The Greedy Catcher Excessive Setup 3 The Sequencer Hidden Dependency The Enumerator The Stranger The Operating System Evangelist Success Against All Odds The Free Ride The One The Peeping Tom The Slow Poke 6 -
The Liar 4 The Giant 5 The Mockery 1 The
Inspector 7 Generous Leftovers 5 The Local Hero 7 The Nitpicker The Secret Catcher The Dodger The Loudmouth Anti patterns The Greedy Catcher Excessive Setup 3 The Sequencer Hidden Dependency The Enumerator The Stranger The Operating System Evangelist Success Against All Odds The Free Ride The One The Peeping Tom The Slow Poke 6 -
Anti patterns - Survey takeaways 1. Survey notes: Javascript, PHP
and Java were the most used programming languages 2. Survey notes: Practitioners usually informally learn TDD 3. Survey notes: Companies argue that TDD requires more time to complete a task and the team don't have time for it -
2. Anti-patterns - Episode 3 The nitpicker, The secret catcher,
The dodger and The Loudmounth Getting started -
The Liar The Giant The Mockery The Inspector Generous Leftovers
The Local Hero The Nitpicker The Secret Catcher The Dodger The Loudmouth Anti patterns The Greedy Catcher Excessive Setup The Sequencer Hidden Dependency The Enumerator The Stranger The Operating System Evangelist Success Against All Odds The Free Ride The One The Peeping Tom The Slow Poke James Carr - TDD Anti-Patterns -
The Liar The Giant The Mockery The Inspector Generous Leftovers
The Local Hero The Nitpicker8 The Secret Catcher7 The Dodger8 The Loudmouth8 Anti patterns The Greedy Catcher Excessive Setup The Sequencer Hidden Dependency The Enumerator The Stranger The Operating System Evangelist Success Against All Odds The Free Ride The One The Peeping Tom The Slow Poke James Carr - TDD Anti-Patterns -
2. The nitpicker - 🏆8 A unit test which compares
a complete output when it’s really only interested in small parts of it, so the test has to continually be kept in line with otherwise unimportant details. Endemic in web application testing. Crafting code -
public function testDeleteApplication() { $response = $this->postApplication(); $this->assertFalse($response->error); $this->delete('api/application/' .
$response->data) ->assertExactJson([ 'data' => (string) $response->data, 'error' => false ]); } PHP - Laravel -
public function testDeleteApplication() { $response = $this->postApplication(); $this->assertFalse($response->error); $this->delete('api/application/' .
$response->data) ->assertExactJson([ 'data' => (string) $response->data, 'error' => false ]); } PHP - Laravel -
public function testDeleteApplication() { $response = $this->postApplication(); $this->assertFalse($response->error); $this->delete('api/application/' .
$response->data) ->assertExactJson([ 'data' => (string) $response->data, 'error' => false ]); } PHP - Laravel -
public function testDeleteApplication() { $response = $this->postApplication(); $this->assertFalse($response->error); $this->delete('api/application/' .
$response->data) ->assertExactJson([ 'data' => (string) $response->data, 'error' => false ]); } PHP - Laravel -
public function testDeleteApplication() { $response = $this->postApplication(); $this->assertFalse($response->error); $this->delete('api/application/' .
$response->data) ->assertExactJson([ 'data' => (string) $response->data, 'error' => false ]); } PHP - Laravel -
public function testDeleteApplication() { $response = $this->postApplication(); $this->assertFalse($response->error); $this->delete('api/application/' .
$response->data) ->assertExactJson([ 'data' => (string) $response->data, 'error' => false ]); } PHP - Laravel -
public function testDeleteApplication() { $response = $this->postApplication(); $this->assertFalse($response->error); $this->delete('api/application/' .
$response->data) ->assertExactJson([ 'data' => (string) $response->data, 'error' => false ]); } PHP - Laravel -
public function testDeleteApplication() { $response = $this->postApplication(); $this->assertFalse($response->error); $this->delete('api/application/' .
$response->data) ->assertJson([ 'data' => (string) $response->data, 'error' => false ]); } PHP - Laravel -
public function testDeleteApplication() { $response = $this->postApplication(); $this->assertFalse($response->error); $this->delete('api/application/' .
$response->data) ->assertJson([ 'data' => (string) $response->data, 'error' => false ]); } PHP - Laravel -
describe('#getSignedCookies()', function() { it('should create cookies object', function(done) { var
result = CloudfrontUtil.getSignedCookies( 'http://foo.com', defaultParams); expect(result).to.have.property('CloudFront-Policy'); expect(result).to.have.property('CloudFront-Signature'); expect(result).to.have.property('CloudFront-Key-Pair-Id'); done(); }); }); Javascript - chai -
describe('#getSignedCookies()', function() { it('should create cookies object', function(done) { var
result = CloudfrontUtil.getSignedCookies( 'http://foo.com', defaultParams); expect(result).to.have.property('CloudFront-Policy'); expect(result).to.have.property('CloudFront-Signature'); expect(result).to.have.property('CloudFront-Key-Pair-Id'); done(); }); }); Javascript - chai -
describe('#getSignedCookies()', function() { it('should create cookies object', function(done) { var
result = CloudfrontUtil.getSignedCookies( 'http://foo.com', defaultParams); expect(result).to.have.property('CloudFront-Policy'); expect(result).to.have.property('CloudFront-Signature'); expect(result).to.have.property('CloudFront-Key-Pair-Id'); done(); }); }); Javascript - chai -
describe('#getSignedCookies()', function() { it('should create cookies object', function(done) { var
result = CloudfrontUtil.getSignedCookies( 'http://foo.com', defaultParams); expect(result).to.have.property('CloudFront-Policy'); expect(result).to.have.property('CloudFront-Signature'); expect(result).to.have.property('CloudFront-Key-Pair-Id'); done(); }); }); Javascript - chai -
describe('#getSignedCookies()', function() { it('should create cookies object', function(done) { var
result = CloudfrontUtil.getSignedCookies( 'http://foo.com', defaultParams); expect(result).to.have.property('CloudFront-Policy'); expect(result).to.have.property('CloudFront-Signature'); expect(result).to.have.property('CloudFront-Key-Pair-Id'); done(); }); }); Javascript - chai -
describe('#getSignedCookies()', function() { it('should create cookies object', function(done) { var
result = CloudfrontUtil.getSignedCookies( 'http://foo.com', defaultParams); expect(result).to.have.property('CloudFront-Policy'); expect(result).to.have.property('CloudFront-Signature'); expect(result).to.have.property('CloudFront-Key-Pair-Id'); done(); }); }); Javascript - chai -
describe('#getSignedCookies()', function() { it('should create cookies object', function(done) { var
result = CloudfrontUtil.getSignedCookies( 'http://foo.com', defaultParams); expect(result).to.have.property('CloudFront-Policy'); expect(result).to.have.property('CloudFront-Signature'); expect(result).to.have.property('CloudFront-Key-Pair-Id'); done(); }); }); Javascript - chai -
describe('#getSignedCookies()', function() { it('should create cookies object', function(done) { var
result = CloudfrontUtil.getSignedCookies( 'http://foo.com', defaultParams); expect(result).to.have.property('CloudFront-Policy'); expect(result).to.have.property('CloudFront-Signature'); expect(result).to.have.property('CloudFront-Key-Pair-Id'); done(); }); }); Javascript - chai -
describe('#getSignedCookies()', function() { it('should create cookies object', function(done) { var
result = CloudfrontUtil.getSignedCookies( 'http://foo.com', defaultParams); expect(result).to.have.property('CloudFront-Policy'); expect(result).to.have.property('CloudFront-Signature'); expect(result).to.have.property('CloudFront-Key-Pair-Id'); done(); }); }); Javascript - chai -
describe('#getSignedCookies()', function() { it('should create cookies object', function(done) { var
result = CloudfrontUtil.getSignedCookies( 'http://foo.com', defaultParams); expect(result).to.have.property('CloudFront-Policy'); expect(result).to.have.property('CloudFront-Signature'); expect(result).to.have.property('CloudFront-Key-Pair-Id'); done(); }); }); Javascript - chai -
@Test fun `should calculate CFR correctly by monthly and the
time split works well ( cross a calendar month)`() { val requestBody = """ { skipped code } """.trimIndent() RestAssured .given() .contentType(ContentType.JSON) .body(requestBody) .post("/api/pipeline/metrics") .then() .statusCode(200) .body("changeFailureRate.summary.value", equalTo(30.0F)) .body("changeFailureRate.summary.level", equalTo("MEDIUM")) .body("changeFailureRate.details[0].value", equalTo("NaN")) .body("changeFailureRate.details[1].value", equalTo("NaN")) .body("changeFailureRate.details[2].value", equalTo(30.0F)) } Kotlin - RestAssured -
@Test fun `should calculate CFR correctly by monthly and the
time split works well ( cross a calendar month)`() { val requestBody = """ { skipped code } """.trimIndent() RestAssured .given() .contentType(ContentType.JSON) .body(requestBody) .post("/api/pipeline/metrics") .then() .statusCode(200) .body("changeFailureRate.summary.value", equalTo(30.0F)) .body("changeFailureRate.summary.level", equalTo("MEDIUM")) .body("changeFailureRate.details[0].value", equalTo("NaN")) .body("changeFailureRate.details[1].value", equalTo("NaN")) .body("changeFailureRate.details[2].value", equalTo(30.0F)) } Kotlin - RestAssured -
@Test fun `should calculate CFR correctly by monthly and the
time split works well ( cross a calendar month)`() { val requestBody = """ { skipped code } """.trimIndent() RestAssured .given() .contentType(ContentType.JSON) .body(requestBody) .post("/api/pipeline/metrics") .then() .statusCode(200) .body("changeFailureRate.summary.value", equalTo(30.0F)) .body("changeFailureRate.summary.level", equalTo("MEDIUM")) .body("changeFailureRate.details[0].value", equalTo("NaN")) .body("changeFailureRate.details[1].value", equalTo("NaN")) .body("changeFailureRate.details[2].value", equalTo(30.0F)) } Kotlin - RestAssured -
@Test fun `should calculate CFR correctly by monthly and the
time split works well ( cross a calendar month)`() { val requestBody = """ { skipped code } """.trimIndent() RestAssured .given() .contentType(ContentType.JSON) .body(requestBody) .post("/api/pipeline/metrics") .then() .statusCode(200) .body("changeFailureRate.summary.value", equalTo(30.0F)) .body("changeFailureRate.summary.level", equalTo("MEDIUM")) .body("changeFailureRate.details[0].value", equalTo("NaN")) .body("changeFailureRate.details[1].value", equalTo("NaN")) .body("changeFailureRate.details[2].value", equalTo(30.0F)) } Kotlin - RestAssured -
@Test fun `should calculate CFR correctly by monthly and the
time split works well ( cross a calendar month)`() { val requestBody = """ { skipped code } """.trimIndent() RestAssured .given() .contentType(ContentType.JSON) .body(requestBody) .post("/api/pipeline/metrics") .then() .statusCode(200) .body("changeFailureRate.summary.value", equalTo(30.0F)) .body("changeFailureRate.summary.level", equalTo("MEDIUM")) .body("changeFailureRate.details[0].value", equalTo("NaN")) .body("changeFailureRate.details[1].value", equalTo("NaN")) .body("changeFailureRate.details[2].value", equalTo(30.0F)) } Kotlin - RestAssured -
@Test fun `should calculate CFR correctly by monthly and the
time split works well ( cross a calendar month)`() { val requestBody = """ { skipped code } """.trimIndent() RestAssured .given() .contentType(ContentType.JSON) .body(requestBody) .post("/api/pipeline/metrics") .then() .statusCode(200) .body("changeFailureRate.summary.value", equalTo(30.0F)) .body("changeFailureRate.summary.level", equalTo("MEDIUM")) .body("changeFailureRate.details[0].value", equalTo("NaN")) .body("changeFailureRate.details[1].value", equalTo("NaN")) .body("changeFailureRate.details[2].value", equalTo(30.0F)) } Kotlin - RestAssured -
@Test fun `should calculate CFR correctly by monthly and the
time split works well ( cross a calendar month)`() { val requestBody = """ { skipped code } """.trimIndent() RestAssured .given() .contentType(ContentType.JSON) .body(requestBody) .post("/api/pipeline/metrics") .then() .statusCode(200) .body("changeFailureRate.summary.value", equalTo(30.0F)) .body("changeFailureRate.summary.level", equalTo("MEDIUM")) .body("changeFailureRate.details[0].value", equalTo("NaN")) .body("changeFailureRate.details[1].value", equalTo("NaN")) .body("changeFailureRate.details[2].value", equalTo(30.0F)) } Kotlin - RestAssured -
@Test fun `should calculate CFR correctly by monthly and the
time split works well ( cross a calendar month)`() { val requestBody = """ { skipped code } """.trimIndent() RestAssured .given() .contentType(ContentType.JSON) .body(requestBody) .post("/api/pipeline/metrics") .then() .statusCode(200) .body("changeFailureRate.summary.value", equalTo(30.0F)) .body("changeFailureRate.summary.level", equalTo("MEDIUM")) .body("changeFailureRate.details[0].value", equalTo("NaN")) .body("changeFailureRate.details[1].value", equalTo("NaN")) .body("changeFailureRate.details[2].value", equalTo(30.0F)) } Kotlin - RestAssured -
@Test fun `should calculate CFR correctly by monthly and the
time split works well ( cross a calendar month)`() { val requestBody = """ { skipped code } """.trimIndent() RestAssured .given() .contentType(ContentType.JSON) .body(requestBody) .post("/api/pipeline/metrics") .then() .statusCode(200) .body("changeFailureRate.summary.value", equalTo(30.0F)) .body("changeFailureRate.summary.level", equalTo("MEDIUM")) .body("changeFailureRate.details[0].value", equalTo("NaN")) .body("changeFailureRate.details[1].value", equalTo("NaN")) .body("changeFailureRate.details[2].value", equalTo(30.0F)) } Kotlin - RestAssured -
@Test fun `should calculate CFR correctly by monthly and the
time split works well ( cross a calendar month)`() { val requestBody = """ { skipped code } """.trimIndent() RestAssured .given() .contentType(ContentType.JSON) .body(requestBody) .post("/api/pipeline/metrics") .then() .statusCode(200) .body("changeFailureRate.summary.value", equalTo(30.0F)) .body("changeFailureRate.summary.level", equalTo("MEDIUM")) .body("changeFailureRate.details[0].value", equalTo("NaN")) .body("changeFailureRate.details[1].value", equalTo("NaN")) .body("changeFailureRate.details[2].value", equalTo(30.0F)) } Kotlin - RestAssured -
• Use what you need • It can be generalized
to other applications (CLI for example) Points of attention -
3. The Secret Catcher - 🏆7 A test that at
first glance appears to be doing no testing due to the absence of assertions, but as they say, “the devil’s in the details.” The test is really relying on an exception to be thrown when a mishap occurs, and is expecting the testing framework to capture the exception and report it to the user as a failure. Crafting code -
test('it handles error when removing credit card ', async ()
=> { const data = await Payment.asyncData(asyncDataContext); data.paymentMethod = PaymentMethod.CREDIT_CARD; const { getAllByText } = render(Payment, { mocks, data() { return { ...data }; }, }); const [removeButton] = getAllByText(Remove'); await fireEvent.click(removeButton); }); Jest - Javascript -
test('it handles error when removing credit card ', async ()
=> { const data = await Payment.asyncData(asyncDataContext); data.paymentMethod = PaymentMethod.CREDIT_CARD; const { getAllByText } = render(Payment, { mocks, data() { return { ...data }; }, }); const [removeButton] = getAllByText('Remove'); await fireEvent.click(removeButton); }); Jest - Javascript -
test('it handles error when removing credit card ', async ()
=> { const data = await Payment.asyncData(asyncDataContext); data.paymentMethod = PaymentMethod.CREDIT_CARD; const { getAllByText } = render(Payment, { mocks, data() { return { ...data }; }, }); const [removeButton] = getAllByText('Remove'); await fireEvent.click(removeButton); }); Jest - Javascript -
test('it handles error when removing credit card ', async ()
=> { const data = await Payment.asyncData(asyncDataContext); data.paymentMethod = PaymentMethod.CREDIT_CARD; const { getAllByText } = render(Payment, { mocks, data() { return { ...data }; }, }); const [removeButton] = getAllByText('Remove'); await fireEvent.click(removeButton); }); Jest - Javascript -
test('it handles error when removing credit card ', async ()
=> { const data = await Payment.asyncData(asyncDataContext); data.paymentMethod = PaymentMethod.CREDIT_CARD; const { getAllByText } = render(Payment, { mocks, data() { return { ...data }; }, }); const [removeButton] = getAllByText('Remove'); await fireEvent.click(removeButton); }); Jest - Javascript -
test('it handles error when removing credit card ', async ()
=> { const data = await Payment.asyncData(asyncDataContext); data.paymentMethod = PaymentMethod.CREDIT_CARD; const { getAllByText } = render(Payment, { mocks, data() { return { ...data }; }, }); const [removeButton] = getAllByText('Remove'); await fireEvent.click(removeButton); }); Jest - Javascript -
test('it handles error when removing credit card ', async ()
=> { const data = await Payment.asyncData(asyncDataContext); data.paymentMethod = PaymentMethod.CREDIT_CARD; const { getAllByText } = render(Payment, { mocks, data() { return { ...data }; }, }); const [removeButton] = getAllByText('Remove'); await fireEvent.click(removeButton); }); Jest - Javascript 👀 -
test('it handles error when removing credit card ', async ()
=> { const data = await Payment.asyncData(asyncDataContext); data.paymentMethod = PaymentMethod.CREDIT_CARD; const { getAllByText } = render(Payment, { mocks, data() { return { ...data }; }, }); const [removeButton] = getAllByText('Remove'); await fireEvent.click(removeButton); expect(getByText('Some error')).toBeInTheDocument(); }); Jest - Javascript -
Software engineering unlocked, Dr. McKayla Because well, if, if you
know, a smoke test could also execute a lot of the code base. Actually just, you know, make sure that the code run somehow without verifying that, you know, the inputs and outputs are matching and so on. Dr. Mauricio’s Aniche -
4. The Dodger - 🏆8 A unit test which has
lots of tests for minor (and presumably easy to test) side effects, but never tests the core desired behavior. Sometimes you may find this in database access related tests, where a method is called, then the test selects from the database and runs assertions against the result. Crafting code -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
/** * Tests that objects works as expected. */ public
function testObject(): void { $author = Author::createFromArray($this->getSampleId(), $this->getSampleValues()); $this->assertEquals($this->getSampleId(), $author->getId()); $this->assertEquals($this->getSampleValues()['name']['given'], $author->getNameGiven()); $this->assertEquals($this->getSampleValues()['name']['family'], $author->getNameFamily()); $this->assertEquals($this->getSampleValues()['country'], $author->getCountry()); $this->assertEquals($this->getSampleValues()['org']['name'], $author->getOrgName()); $this->assertEquals($this->getSampleValues()['org']['unit'], $author->getOrgUnit()); $this->assertEquals($this->getSampleValues()['homepage'], $author->getHomepage()); $this->assertEquals($this->getSampleValues()['description'], $author->getDescription()); $this->assertEquals($this->getSampleValues()['image'], $author->getImage()); $this->assertEquals($this->getSampleValues()['identification'], $author->getIdentification()); $this->assertEquals($this->getSampleValues()['identification']['email'], $author->getIdentification('email')); $this->assertEquals([], $author->getIdentification('not exist')); $this->assertEquals($author->checksum(), $author->checksum()); } Drupal - PHP -
• 1-1 one test class to one production class •
Focus on implementation details rather than behavior Points of attention -
5. The Loudmouth - 🏆8 A unit test (or test
suite) that clutters up the console with diagnostic messages, logging messages, and other miscellaneous chatter, even when tests are passing. Crafting code -
test.each([['function']])( 'should not bubble up the error when a invalid
source code is provided', (code) => { const strategy = jest.fn(); const result = Reason(code, strategy); expect(strategy).toHaveBeenCalledTimes(0); expect(result).toBeFalsy(); } ); Console log - Javascript -
test.each([['function']])( 'should not bubble up the error when a invalid
source code is provided', (code) => { const strategy = jest.fn(); const result = Reason(code, strategy); expect(strategy).toHaveBeenCalledTimes(0); expect(result).toBeFalsy(); } ); Console log - Javascript -
test.each([['function']])( 'should not bubble up the error when a invalid
source code is provided', (code) => { const strategy = jest.fn(); const result = Reason(code, strategy); expect(strategy).toHaveBeenCalledTimes(0); expect(result).toBeFalsy(); } ); Console log - Javascript -
test.each([['function']])( 'should not bubble up the error when a invalid
source code is provided', (code) => { const strategy = jest.fn(); const result = Reason(code, strategy); expect(strategy).toHaveBeenCalledTimes(0); expect(result).toBeFalsy(); } ); Console log - Javascript -
const reason = function(code, strategy) { try { const ast
= esprima.parseScript(code); if (ast.body.length > 0) { return strategy(ast); } } catch (error) { /* eslint-disable-next-line */ console.warn(error); return false; } }; Console log - Javascript -
const reason = function(code, strategy) { try { const ast
= esprima.parseScript(code); if (ast.body.length > 0) { return strategy(ast); } } catch (error) { /* eslint-disable-next-line */ console.warn(error); return false; } }; Console log - Javascript -
const reason = function(code, strategy) { try { const ast
= esprima.parseScript(code); if (ast.body.length > 0) { return strategy(ast); } } catch (error) { /* eslint-disable-next-line */ console.warn(error); return false; } }; Console log - Javascript -
const reason = function(code, strategy) { try { const ast
= esprima.parseScript(code); if (ast.body.length > 0) { return strategy(ast); } } catch (error) { /* eslint-disable-next-line */ console.warn(error); return false; } }; Console log - Javascript -
const originalConsole = globalThis.console; beforeEach(() => { globalThis.console = {
warn: jest.fn(), error: jest.fn(), log: jest.fn() }; }); afterEach(() => { globalThis.console = originalConsole; }); Console log - Javascript -
const originalConsole = globalThis.console; beforeEach(() => { globalThis.console = {
warn: jest.fn(), error: jest.fn(), log: jest.fn() }; }); afterEach(() => { globalThis.console = originalConsole; }); Console log - Javascript -
const originalConsole = globalThis.console; beforeEach(() => { globalThis.console = {
warn: jest.fn(), error: jest.fn(), log: jest.fn() }; }); afterEach(() => { globalThis.console = originalConsole; }); Console log - Javascript -
const originalConsole = globalThis.console; beforeEach(() => { globalThis.console = {
warn: jest.fn(), error: jest.fn(), log: jest.fn() }; }); afterEach(() => { globalThis.console = originalConsole; }); Console log - Javascript -
• Clean up if not needed • Threat the logs
as a feature, test drive them Points of attention -
• The nitpicker • The secret catcher • The dodger
• The Loudmounth • and many more! What we covered -
Matheus Marabesi Hello there, you can call me Marabesi, But
my name is Matheus Marabesi, I work at Codurance as a Software craftsperson. I enjoy talking about anything related to: testing, patterns and gamification. You can find me at @MatheusMarabesi or https://marabesi.com Codurance Crafting Code