Test Automation With Python Part 2 PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

Test Automation with

Python
Part 2: Test suites, pre/post conditions, fixtures
Agenda

● Re-cap
● Pre-conditions / cleanups
● Test suites
● Fixtures
● Practice (writing a test suite)
● Running tests
● [if we have time] allure steps
Let’s recap what
we did before
Pytest

Pytest is a framework for writing tests.


Pytest

Pytest is a framework for writing tests.

● Recognizes tests
● Allows to run them through command-line
● Provides assertions
● Provides pre / conditions cleanups
● Logging / reporting / tagging support
Let’s install it
# to install something, the best way is to use pip:

$ pip install <package name>

# to install multiple dependencies, we can use a file:

$ pip install -r <file_with_dependencies.txt>


Let’s install it

1. Create a file named “requirements.txt” in the top folder


2. Then, add pytest into the file
3. Install:

Option 1: open requirements.txt in PyCharm and click “install requirements” in the


upper right corner.

Option 2:

# then, run:

$ pip install -r requirements.txt


Let’s set it up in PyCharm

In “File -> Settings”:


To check it was installed
# run:

$ pip freeze

# if you are on linux, you can grep :)

$ pip freeze | grep pytest


How to write tests with pytest

There are multiple ways. Today, we’ll use functions for tests.
How to write tests with pytest

A function must start with the word test. This way, pytest will recognize the test
and be able to run it:
How to run the tests
$ pytest
# OR:
$ pytest /path/to/test
# OR:
Just click the green arrow near the test in PyCharm :)
# OR:
Just right-click on the test file surface and select “run
pytest in ...”
Let’s practice a bit
Re-write your test so that it’s
recognized by pytest.
Pre-conditions / cleanups
Pre-conditions / cleanups

The easiest way to create a pre-condition / cleanup in pytest is to use fixtures


What is a fixture

Fixture is a python decorator. It allows doing things before / after the test, or
provide values to a test.
Fixture - providing a value
Fixture - doing something before test
Fixture - doing something after test
Fixture - scope

Scope lets you specify what part of the tests a fixture should be applied to.

There are multiple scope options:

- Default (when you don’t specify scope) is per test;


- Module - applied before / after whole module;
- Session - applied once before all tests;
- Dynamic - more complicated stuff! You can dynamically specify the scope
depending on the situation.
Fixture - module scope
Fixture - session scope
Result looks the same for our case, but will
be applied before ALL tests in the framework
once.
Fixture - scope can also serve as caching
Fixture - scope can also serve as caching
conftest

conftest.py is a special kind of file recognized by pytest. It allows to define


fixtures that are globally available.
conftest - how to
conftest - rules

● You can define conftests on different “levels” of your framework structure


conftest - rules

● You can define conftests on different “levels” of your framework structure


● The upper-level conftest will be visible by all tests
conftest - rules

● You can define conftests on different “levels” of your framework structure


● The upper-level conftest will be visible by all tests
● Lower-level conftest will NOT apply to upper level tests
● If you name a fixture the same way as in the upper level conftest, it will be re-
written by the one in the lower level
conftest - rules

● You can define conftests on different “levels” of your framework structure


● The upper-level conftest will be visible by all tests
● Lower-level conftest will NOT apply to upper level tests
Time for
practice!
Create tests for car.py

● Add your test file(s) into tests/test_second_episode/


● Test file name should start with the word “test_”
● Test functions should start with the word “test_”
● Please, add at least one fixture
● If you want, you can create conftest.py
● If you want, you can add multiple test files / folders / conftests on different
levels
● If you have any questions, please tell about it, I’ll show you how to do it right
away :)
Motivational homework

Cover other functions in the file with tests.

Try committing your code and making a pull request.


What’s next

● how to write tests using OOP


● layers of testing framework
● unittest
● setUp/tearDown, setUpClass, tearDownClass
● base class
● practice (writing base class and tests)
Q&A

You might also like