automated software testing - software...

38
Automated Software Testing Maurício Aniche [email protected] @mauricioaniche Delft University of Technology, 2019

Upload: others

Post on 27-May-2020

49 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Automated Software TestingMaurício Aniche

[email protected]@mauricioaniche

Delft University of Technology, 2019

Page 2: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Agile is not only about processes…

• Technical practices are also very important:• Automated testing• Test-Driven Development• Pair programming• Refactoring• Continuous Integration

• Extreme Programming (XP) has a strong focus on the technical aspects of agility.

Page 3: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Agile is not only about processes…

• Technical practices are also very important:• Automated testing• Test-Driven Development• Pair programming• Refactoring• Continuous Integration

• Extreme Programming (XP) has a strong focus on the technical aspects of agility.

Page 4: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Yes, I can do it!!

Given a list of numbers, I want to know the smallest and the largest numbers there!

Page 5: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

def find(nums):smallest = 9999largest = -9999for num in nums:

if(num < smallest):smallest = num

elif(num > largest):largest = num

print(smallest)print(largest)

find([29, 37, 4, 5])

Page 6: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Ø 4Ø 37

Yes, it works!

Page 7: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl
Page 8: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

find([29, 28, 27, 26])

Ø 26Ø -9999

My bad…. :/If I pass {29, 28, 27, 26}, the program gets crazy!!

Page 9: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

def find(nums):smallest = 9999largest = -9999for num in nums:

if(num < smallest):smallest = num

elif(num > largest):largest = num

print(smallest)print(largest)

find([29, 37, 4, 5])

Page 10: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

def find(nums):smallest = 9999largest = -9999for num in nums:

if(num < smallest):smallest = num

if(num > largest):largest = num

return min, maxmin, max = find([29, 37, 4, 5])print(min)print(max)

Page 11: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Have you made such mistakes before?

Beller, Moritz, Andy Zaidman, and Andrey Karpov. "The last line effect." Proceedings of the 2015 IEEE 23rd International Conference on Program Comprehension. IEEE Press, 2015.Beller, M., Zaidman, A., Karpov, A., & Zwaan, R. A. (2017). The last line effect explained. Empirical Software Engineering, 22(3), 1508-1536.

Page 12: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

A (bad) fairy tale

Page 13: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

What’s the solution for that?

Test your software!!

Page 14: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

What’s wrong with manual testing?

• Too slow• Too expensive• Not easy to reproduce• Susceptible to failures• … boring!

Page 15: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Let’s automate some tests!

• Unit testing framework• Pytest!

• Triple A (Arrange-Act-Assert)

•Let’s do it!

Page 16: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

def test_decreasing_order():min, max = find([29, 28, 27, 26])

assert min == 26assert max == 29

def test_any_random_numbers():min, max = find([29, 37, 4, 5])

assert min == 4assert max == 37

https://gist.github.com/mauricioaniche/73366e8e00686e3d44cb48f971599716

Page 17: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Source code

• Implementation here:• https://gist.github.com/mauricioaniche/9735db28d7091dac0f701dac

a327313a

Page 18: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

”But if you write 100 lines of production code,now you’ll write only 50, as the other 50 are testing. Therefore, you are less productive.”

– says a bad manager.

Page 19: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Not true.

• You spend a lot of time in manual testing.• Now, you will spend it only once: to write the test.

• Teams with automated test suites spend less time debugging.

George, B., Williams, L., An Initial Investigation of TDD in Industry. ACM Symposium on Applied Computing. Melbourne, Florida, USA, 2003.Janzen, D., Software Architecture Improvement through Test-Driven Development. Conference on Object Oriented Programming Systems Languages and Applications, ACM, 2005

Page 20: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Why is a failing test a good thing?

• If it fails here, it didn’t fail in production! J• You have the possibility to fix the bug before going to production.• ”If you have a test that never fails, you have a useless test.” –

pragmatic developer.• Always see your test failing at least once.

• How do I test the test?

Page 21: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

The bug cycle

• You found a bug!• Write a test to make sure it exists• Fix the bug• The bug will never haunt you again.

Page 22: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

FIRST!

• Fast• Isolated• Repeatable• Self-validating• Timely

Page 23: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Fast

• If they are fast, you run them all day long.• If they are not, you stop using them.• Make your tests fast!

• Unit tests tend to be fast• Mock dependencies (Curious? Google for it! Or ask Steve Freeman!)

Page 24: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Isolated

• A test should not depend on another test.• Test dependencies lead to flaky tests + hard to diagnose failures.

• Isolate your tests as much as you can.

Page 25: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Repeatable

• Tests that produce different results in different executions are hard to diagnose.

• If you run your test twice, the results should be the same.

• Write small, focused, isolated tests to avoid such issue.

Page 26: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Self-validating

• Your tests should be 100% automated.• If you need a ”small manual test” to arrange or to assert the test, it is not

automated anymore.

• We want our tests to be automatically executed by machines without any human intervention.

Page 27: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Timely

• You should write tests all day long.• Do not wait until the end to start writing tests.• Your team should understand the importance of such thing.

• ”peer pressure” helps.

Page 28: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

A ATest

B BTest

C CTest

That’s what we call Unit Testing.

Page 29: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Some definitions

• ISQTB: “Searches for defects in, and verifies the functioning of software items (e.g., modules, programs, objects, classes, etc) that are separately testable”.

• Osherove: “A unit test is an automated piece of code that invokes a unit of work in the system and then checks a single assumption about the behavior of that unit of work. [...] A unit of work is a single logical functional use case in the system that can be invoked by some public interface (in most cases). A unit of work can span a single method, a whole class or multiple classes working together to achieve one single logical purpose that can be verified.”

Page 30: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Advantages Disadvantages

• Very fast• Easy to control• Easy to write

• Less real• Some bugs can’t be

reproduced at such level

Page 31: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

When do we need more reality?What can we do to gain reality?

Page 32: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

BigFeatureTest

We can do System Testing!

Page 33: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Advantages Disadvantages

• Very realistic• Captures the user

perspective

• Slow• Hard to write• Flaky

Page 34: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Testing pyramid

Unit tests

Integration tests

System tests

Manual

Mor

e re

ality

Mor

e co

mpl

exity

Page 35: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

How I (Maurício) do the trade-off

All business rules should betested here.

Avoid at all cost. But do itwhen needed.

Complex integrations with external services.

Main/Risky flow of the apptested.

Unit tests

Integration tests

System tests

Manual

Page 36: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

”Whenever you are tempted to type something into a print statement or a debugger expression, write it as a test instead”.

--- Martin Fowler

Page 37: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

License

• You can use and share any of my material (lecture slides, website).• You always have to give credits to the original author.• You agree not to sell it or make profit in any way with this.

• Material that I refer has its own license. Please check it out.

Page 38: Automated Software Testing - Software engineeringse.ewi.tudelft.nl/ti3115tu-2019/resources/06-automated-sw-testing.pdf · Automated Software Testing MaurícioAniche m.f.aniche@tudelft.nl

Images in this presentation

• Female engineer by GDJ: https://openclipart.org/detail/230143/female-engineer-9

• Lady bug, by Scout: https://openclipart.org/detail/191074/lady-bug• Business man, by GDJ:

https://openclipart.org/detail/279728/business-presentation• Rocket explosion, by bf5man:

https://openclipart.org/detail/203747/rocket-explosion