Slide 1

Slide 1 text

TDD - EP 3 codurance.com Testing anti-patterns - The nitpicker, The secret catcher, The dodger, The Loudmouth

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

1. Intro - Recap 2. The nitpicker 3. The secret catcher 4. The dodger 5. The Loudmouth 6. Wrapping up Crafting code Agenda

Slide 4

Slide 4 text

1. Recap Episode 1, Episode 2 Getting started

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

2. Anti-patterns - Episode 3 The nitpicker, The secret catcher, The dodger and The Loudmounth Getting started

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

Survey

Slide 13

Slide 13 text

Survey 54.6% Agree

Slide 14

Slide 14 text

Survey 45.4% Neutral + disagree

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

@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

Slide 36

Slide 36 text

@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

Slide 37

Slide 37 text

@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

Slide 38

Slide 38 text

@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

Slide 39

Slide 39 text

@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

Slide 40

Slide 40 text

@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

Slide 41

Slide 41 text

@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

Slide 42

Slide 42 text

@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

Slide 43

Slide 43 text

@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

Slide 44

Slide 44 text

@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

Slide 45

Slide 45 text

● Use what you need ● It can be generalized to other applications (CLI for example) Points of attention

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

Reflection - Java

Slide 48

Slide 48 text

Reflection - Java

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

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

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

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 👀

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

Reflection - Java

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

● Context matters Points of attention

Slide 60

Slide 60 text

● Lack of practice on TDD ● Oriented to coverage Root cause

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

http://blog.cleancoder.com/uncle-bob/2017/10/03/TestContravariance.html

Slide 63

Slide 63 text

/** * 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

Slide 64

Slide 64 text

/** * 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

Slide 65

Slide 65 text

/** * 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

Slide 66

Slide 66 text

/** * 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

Slide 67

Slide 67 text

/** * 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

Slide 68

Slide 68 text

/** * 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

Slide 69

Slide 69 text

/** * 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

Slide 70

Slide 70 text

/** * 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

Slide 71

Slide 71 text

/** * 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

Slide 72

Slide 72 text

/** * 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

Slide 73

Slide 73 text

/** * 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

Slide 74

Slide 74 text

/** * 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

Slide 75

Slide 75 text

/** * 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

Slide 76

Slide 76 text

/** * 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

Slide 77

Slide 77 text

/** * 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

Slide 78

Slide 78 text

/** * 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

Slide 79

Slide 79 text

/** * 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

Slide 80

Slide 80 text

● 1-1 one test class to one production class ● Focus on implementation details rather than behavior Points of attention

Slide 81

Slide 81 text

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

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

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

Slide 87

Slide 87 text

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

Slide 88

Slide 88 text

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

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

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

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

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

Slide 94

Slide 94 text

const originalConsole = globalThis.console; beforeEach(() => { globalThis.console = { warn: jest.fn(), error: jest.fn(), log: jest.fn() }; }); afterEach(() => { globalThis.console = originalConsole; }); Console log - Javascript

Slide 95

Slide 95 text

const originalConsole = globalThis.console; beforeEach(() => { globalThis.console = { warn: jest.fn(), error: jest.fn(), log: jest.fn() }; }); afterEach(() => { globalThis.console = originalConsole; }); Console log - Javascript

Slide 96

Slide 96 text

const originalConsole = globalThis.console; beforeEach(() => { globalThis.console = { warn: jest.fn(), error: jest.fn(), log: jest.fn() }; }); afterEach(() => { globalThis.console = originalConsole; }); Console log - Javascript

Slide 97

Slide 97 text

const originalConsole = globalThis.console; beforeEach(() => { globalThis.console = { warn: jest.fn(), error: jest.fn(), log: jest.fn() }; }); afterEach(() => { globalThis.console = originalConsole; }); Console log - Javascript

Slide 98

Slide 98 text

● Clean up if not needed ● Threat the logs as a feature, test drive them Points of attention

Slide 99

Slide 99 text

6. Wrapping up We are almost done! Crafting code

Slide 100

Slide 100 text

● The nitpicker ● The secret catcher ● The dodger ● The Loudmounth ● and many more! What we covered

Slide 101

Slide 101 text

https://www.codurance.com/publications/tdd-anti-patterns-chapter-1 https://www.codurance.com/publications/tdd-anti-patterns-chapter-2

Slide 102

Slide 102 text

https://www.codurance.com/publications/building-testing-culture https://www.codurance.com/publications/building-testing-culture

Slide 103

Slide 103 text

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