-
Notifications
You must be signed in to change notification settings - Fork 0
pytest
jangsoohoon edited this page May 12, 2021
·
3 revisions
pyTest to run in verbose mode
A verbose mode is an option available in many computer operating systems, including Microsoft Windows, macOS, and Linux. It provides additional details as to what the computer is doing and what drivers and software it is loading during startup. This level of detail can be helpful for troubleshooting problems with hardware or software upon startup or after the operating system has loaded. Below is an example of verbose output in a Windows command line screen.
- Pytest will automatically discover tests when you execute based on a standard naming convention.
- Test functions should include "test" at the beginning of the function name.
- Classes with tests in them should have "Test" at the beginning of the class name and not have an "init" method
- Filenames of test modules should start or end with "test". (ie. test_example.py or example_test.py)
- XUnit style setup/teardown functions will execute code before and after
- Test Modules
- def setup_module():
- def teardown_module():
- Test Functions
- def setup_function():
- def teardown_function():
- Test Classes
- def setup_class():
- def teardown_class():
- Test Methods in Test classes
- def setup_method():
- def teardown_method():
- Test Modules
- Test Fixtures allow for re-use of setup and teardown code across tests.
- The "pytest.fixture" decorator is applied to functions that are decorators
- Individual unit tests can specify which fixtures they want executed
- the autouse parameter can be set to true to automatically execute a fixture before each test.
execute fixture indivisually
import pytest
@pytest.fixture()
def setup():
print("\nSetup")
def test1(setup):
print("Execute test1")
assert True
@pytest.mark.usefixtures("setup")
def test2():
print("Execute test2")
assert True
execute fixture in all function
import pytest
@pytest.fixture(autouse=True)
def setup():
print("\nSetup")
def test1():
print("Execute test1")
assert True
def test2():
print("Execute test2")
assert True
- Test Fixture can each have their own optional teardown code which is called after a fixture goes out of scope
- There are two methods for specifying teardown code. the "yield" keyword and the request-context object's "addfinalizer" method
- when the "yield" keyword is used the code after the yield is executed after the fixture goes out of scope.
- The "yield" keyword is a replacement for the return keyword so any return values are also specified in the yield statement.
@pytest.fixture():
def setup():
print("Setup")
yield
print("Teardown!")
- With the addfinalizer method a teardown method is defined added via the request-context's addfinalizer method.
- Multiple finalization functions can be specified.
@pytest.fixture():
def setup(request):
print("Setup!")
def teardown:
print("Teardown!")
request.addfinalizer(teardown)
- Test Fixtures can have the following four different scopes which specify how often the fixture will be called:
- Fuction - Run the fixture once for each test
- Class - Run the fixture once for each class of tests
- Module - Run once when the module goes in scope
- Session - The fixture is run when pytest starts
- Test Fixtures can optionally return data which can be used in the test
- The optional "params" array argument in the fixture decorator can be used to specify the data returned to the test.
- when a "params" argument is specified then the test will be called one time with each value specified.
@pytest.fixture(params=[1,2])
def setupData(request):
return request.param
def test1(setupData):
print(setupData)