Chapter 6
■
tips and triCks
107
Finally, Listing
6-7
shows how to add two more test cases to the test module to check
if add() is behaving as expected.
Listing 6-7. test_module01.py
from calculator import Calculator
import pytest
class TestClass01:
def test_case01(self):
calc = Calculator()
result = calc.add(2, 2)
assert 4 ==
result
def test_case02(self):
with pytest.raises(ValueError):
result = Calculator().add(2, 'two')
def test_case03(self):
with pytest.raises(ValueError):
result = Calculator().add('two', 2)
def test_case04(self):
with pytest.raises(ValueError):
result = Calculator().add('two', 'two')
When you run the test module in Listing
6-7
, you will get the following output:
============================= test session starts =============================
platform linux -- Python 3.4.2, pytest-3.0.4, py-1.4.31, pluggy-0.4.0 -- /
usr/bin/python3
cachedir: .cache
rootdir: /home/pi/book/code/chapter06, inifile:
collected 4 items
test_module01.py::TestClass01::test_case01 PASSED
test_module01.py::TestClass01::test_case02 PASSED
test_module01.py::TestClass01::test_case03 PASSED
test_module01.py::TestClass01::test_case04 PASSED
========================== 4 passed in 0.14 seconds ===========================
This is how TDD is implemented in real-life projects.
You write a failing test first,
refactor the development code, and continue the same process until the test passes.
When you want to add a new feature, you repeat this process to implement it.
Chapter 6
■
tips and triCks
108
Conclusion
In this chapter, you learned the coding and filename conventions for easy test discovery;
these conventions can be implemented across all the automation frameworks. You also
read a brief introduction to TDD.
You began the book
with an introduction to Python, including how to install it
on the various OSs, and differences between Python version 2 and version 3. Then,
in subsequent chapters, you explored the most used test automation frameworks for
Python.
In
Chapter
2
, you explored docstrings and how they are useful in writing doctests.
You learned that the doctest is not
a very powerful test framework, as it lacks many
essentials of a true test framework.
In Chapter
3
, we were introduced to Python's batteries-included test automation
framework, unittest. You learned how to write xUnit-style test cases for Python with
unittest.
In Chapter
4
,
you explored a more advanced, but defunct, test automation
framework called nose. You learned about the advanced features and plugins offered by
nose. Because nose is not under active development, you used nose2 as a test-runner for
running nose and unittest tests.
In Chapter
5
, you studied and explored one of the best unit test automation
frameworks for Python, pytest. You learned how and why it is better than unittest and
nose. You also explored its plugins and modular fixtures.
You have practiced numerous examples throughout the book, the goal of which is to
instill you with confidence in Python test automation. You can
also work with codebases
where they have implemented test automation with unittest, doctest, or nose and plan
a migration to pytest. Also, if you are a career Python developer or an automation expert,
you can follow the TDD approach in your projects. I hope all of you have enjoyed reading
this book as much as I enjoyed writing it. Happy Pythoning and testing!!
109
© Ashwin Pajankar 2017
A. Pajankar,
Python Unit Test Automation, DOI 10.1007/978-1-4842-2677-3
A
add() method, 35
assertEqual() method, 35, 55
assertRaises() method, 60–62
assertTrue(), 55
Automated unit testing, 20
Do'stlaringiz bilan baham: