Skip to content

Commit

Permalink
Change example test on default template (#4368)
Browse files Browse the repository at this point in the history
* Change example test on default template

Signed-off-by: Laura Couto <[email protected]>

* Lint

Signed-off-by: Laura Couto <[email protected]>

* Account for extra files in tests

Signed-off-by: Laura Couto <[email protected]>

* Adapt unit tests to new template

Signed-off-by: Laura Couto <[email protected]>

* Adapt e2e test

Signed-off-by: Laura Couto <[email protected]>

* Change test to work with no example pipeline

Signed-off-by: Laura Couto <[email protected]>

* Change test_a_project.md example test

Signed-off-by: Laura Couto <[email protected]>

* Lint

Signed-off-by: Laura Couto <[email protected]>

* Update docs/source/development/automated_testing.md

Co-authored-by: Merel Theisen <[email protected]>
Signed-off-by: L. R. Couto <[email protected]>

---------

Signed-off-by: Laura Couto <[email protected]>
Signed-off-by: L. R. Couto <[email protected]>
Co-authored-by: Merel Theisen <[email protected]>
  • Loading branch information
lrcouto and merelcht authored Dec 6, 2024
1 parent b1b7f13 commit 50e0cb5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 57 deletions.
35 changes: 12 additions & 23 deletions docs/source/development/automated_testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,39 +85,28 @@ tests

### Create an example test

Now that you have a place to put your tests, you can create an example test in the new file `/src/tests/test_run.py`. The example test simply checks that the project_path attribute of a specially-defined `KedroContext` object has been correctly set.
Now that you have a place to put your tests, you can create an example test in the new file `/src/tests/test_run.py`.
This example test demonstrates how to programmatically execute a `kedro run` using the `KedroSession` class.

```
import pytest
from kedro.config import OmegaConfigLoader
from kedro.framework.context import KedroContext
from kedro.framework.hooks import _create_hook_manager
from pathlib import Path
from kedro.framework.session import KedroSession
from kedro.framework.startup import bootstrap_project
class TestKedroRun:
def test_kedro_run(self):
bootstrap_project(Path.cwd())
@pytest.fixture
def config_loader():
return OmegaConfigLoader(conf_source=str(Path.cwd()))
@pytest.fixture
def project_context(config_loader):
return KedroContext(
package_name=<package_name>,
project_path=Path.cwd(),
config_loader=config_loader,
hook_manager=_create_hook_manager(),
)
class TestProjectContext:
def test_project_path(self, project_context):
assert project_context.project_path == Path.cwd()
with KedroSession.create(project_path=Path.cwd()) as session:
assert session.run() is not None
```

This test is redundant, but it introduces a few of `pytest`'s core features and demonstrates the layout of a test file:
- [Fixtures](https://docs.pytest.org/en/7.1.x/explanation/fixtures.html#about-fixtures) are used to define resources used in tests.
- Tests are implemented in methods or functions beginning with `test_` and classes beginning with `Test`.
- The `assert` statement is used to compare the result of the test with an expected value.

Although this specific example does not utilise fixtures, they are an essential part of pytest for defining reusable resources across tests. See [Fixtures](https://docs.pytest.org/en/7.1.x/explanation/fixtures.html#about-fixtures)

Tests should be named as descriptively as possible, especially if you are working with other people. For example, it is easier to understand the purpose of a test with the name `test_node_passes_with_valid_input` than a test with the name `test_passes`.

You can read more about the [basics of using `pytest` on the getting started page](https://docs.pytest.org/en/7.1.x/getting-started.html). For help writing your own tests and using all of the features of `pytest`, see the [project documentation](https://docs.pytest.org/).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
"""
This module contains an example test.
This module contains example tests for a Kedro project.
Tests should be placed in ``src/tests``, in modules that mirror your
project's structure, and in files named test_*.py. They are simply functions
named ``test_*`` which test a unit of logic.
To run the tests, run ``pytest`` from the project root directory.
project's structure, and in files named test_*.py.
"""

from pathlib import Path

import pytest

from kedro.config import OmegaConfigLoader
from kedro.framework.context import KedroContext
from kedro.framework.hooks import _create_hook_manager
from kedro.framework.project import settings


@pytest.fixture
def config_loader():
return OmegaConfigLoader(conf_source=str(Path.cwd() / settings.CONF_SOURCE))


@pytest.fixture
def project_context(config_loader):
return KedroContext(
package_name="{{ cookiecutter.python_package }}",
project_path=Path.cwd(),
env="local",
config_loader=config_loader,
hook_manager=_create_hook_manager(),
)

from pathlib import Path
from kedro.framework.session import KedroSession
from kedro.framework.startup import bootstrap_project

# The tests below are here for the demonstration purpose
# and should be replaced with the ones testing the project
# functionality
class TestProjectContext:
def test_project_path(self, project_context):
assert project_context.project_path == Path.cwd()

class TestKedroRun:
def test_kedro_run_no_pipeline(self):
# This example test expects a pipeline run failure, since
# the default project template contains no pipelines.
bootstrap_project(Path.cwd())

with pytest.raises(Exception) as excinfo:
with KedroSession.create(project_path=Path.cwd()) as session:
session.run()

assert "Pipeline contains no nodes" in str(excinfo.value)

0 comments on commit 50e0cb5

Please sign in to comment.