|
1 | 1 | # Testing packages |
2 | 2 |
|
3 | 3 | When you are developing a package, you should write tests for it. The tests are |
4 | | -automatically executed right after the package build has finished. |
| 4 | +automatically executed as soon as the package build and all it's run dependencies |
| 5 | +are ready. |
5 | 6 |
|
6 | | -The tests from the test section are actually packaged _into_ your package and |
7 | | -can also be executed straight from the existing package. |
| 7 | +## Writing tests |
8 | 8 |
|
9 | | -The idea behind adding the tests into the package is that you can execute the |
10 | | -tests independently from building the package. That is also why we are shipping |
11 | | -a `test` subcommand that takes as input an existing package and executes the |
12 | | -tests: |
| 9 | +You can add one or more tests to your package in the `tests` section of the recipe (or output). |
| 10 | +Each test is run independently, in a separate environment. |
13 | 11 |
|
14 | | -```bash |
15 | | -rattler-build test --package-file ./xtensor-0.24.6-h60d57d3_0.tar.bz2 |
16 | | -``` |
17 | | - |
18 | | -Running the above command will extract the package and create a clean |
19 | | -environment where the package and dependencies are installed. Then the tests are |
20 | | -executed in this newly-created environment. |
| 12 | +One notable difference are the `package_contents` tests that are executed right after the package |
| 13 | +is prepared and do not create a new environment (as we only analyze the contents of the package). |
21 | 14 |
|
22 | | -If you inspect the package contents, you would find the test files under |
23 | | -`info/test/*`. |
| 15 | +```yaml title="recipe.yaml" |
| 16 | +tests: |
| 17 | + # commands to run to test the package. If any of the commands |
| 18 | + # returns with an error code, the test is considered failed. |
| 19 | + - script: |
| 20 | + - echo "Hello world" |
| 21 | + - exit 1 # this will fail the test |
24 | 22 |
|
25 | | -## How tests are translated |
| 23 | + # run a script from the recipe folder |
| 24 | + - script: sometest.py |
26 | 25 |
|
27 | | -The `tests` section allows you to specify the following things: |
| 26 | + # run a Python script with the Python interpreter |
| 27 | + - script: |
| 28 | + interpreter: python |
| 29 | + content: | |
| 30 | + import mypkg |
| 31 | + assert mypkg.__version__ == "0.1.0" |
28 | 32 |
|
29 | | -```yaml |
30 | | -tests: |
| 33 | + # execute `pytest` with the tests from the `tests` folder |
31 | 34 | - script: |
32 | | - # commands to run to test the package. If any of the commands |
33 | | - # returns with an error code, the test is considered failed. |
34 | | - - echo "Hello world" |
35 | 35 | - pytest ./tests |
36 | | - |
37 | 36 | # additional requirements at test time |
38 | 37 | requirements: |
39 | 38 | run: |
40 | 39 | - pytest |
41 | | - |
| 40 | + - python 3.9.* # require an older python version |
| 41 | + # extra files to be copied to the test folder from the recipe or source directory |
42 | 42 | files: |
43 | | - # Extra files to be copied to the test directory from the "work directory" |
44 | | - source: |
45 | | - - tests/ |
46 | | - - test.py |
47 | | - - *.sh |
48 | 43 | recipe: |
49 | | - - more_tests/*.py |
| 44 | + - tests/ |
50 | 45 |
|
51 | | - # This test section tries to import the Python modules and errors if it can't |
| 46 | + # python specific tests |
52 | 47 | - python: |
| 48 | + # this test section tries to import the python modules and errors if it can't |
53 | 49 | imports: |
54 | 50 | - mypkg |
55 | | - - mypkg.subpkg |
| 51 | + pip_check: true |
| 52 | + python_version: [3.9.*, 3.10.*] # run against multiple older python versions |
| 53 | + |
| 54 | + - r: |
| 55 | + libraries: |
| 56 | + - dplyr |
| 57 | + |
| 58 | + - perl: |
| 59 | + modules: |
| 60 | + - JSON |
| 61 | + |
| 62 | + # test the contents of the package. If any of the globs does not match _any_ file, the test fails. |
| 63 | + - package_contents: |
| 64 | + files: |
| 65 | + - share/package/*.txt |
56 | 66 | ``` |
57 | 67 |
|
58 | | -When you are writing a test for your package, additional files are created and |
59 | | -added to your package. These files are placed under the `info/tests/{index}/` |
60 | | -folder for each test. |
| 68 | +### Testing package contents |
| 69 | +
|
| 70 | +The `package_contents` test is a special test that is executed right after the |
| 71 | +package is prepared. It does not create a new environment, but instead checks the paths that will be part of the final package. |
| 72 | +It can be very useful as a "sanity check" to ensure that the package contains the expected files. |
| 73 | + |
| 74 | +It has multiple sub-keys that help when building cross-platform packages: |
| 75 | + |
| 76 | +- **`files`**: a list of globs that should match at least one file in the package. If any of the globs do not match, the test fails. |
| 77 | +- **`lib`**: matches libraries in the package (`.so`, `.dll`, `.dylib` files). The test fails if any of the libraries are not found. It's enough to specify the library name without any extension (e.g. `foo` will match `libfoo.so`, `libfoo.dylib`, and `foo.dll`). |
| 78 | +- **`include`**: matches files under the `include` directory in the package. You can specify the file name like `foo.h`. |
| 79 | +- **`bin`**: matches files under the `bin` directory in the package. You can specify executable names like `foo` which will match `foo.exe` on Windows and `foo` on Linux and macOS. |
| 80 | +- **`site_packages`**: matches files under the `site-packages` directory in the package. You can specify the import path like `foobar.api` which will match `foobar/api.py` and `foobar/api/__init__.py`. |
| 81 | + |
| 82 | +## Testing existing packages |
61 | 83 |
|
62 | | -For a script test: |
| 84 | +The tests from the test section are actually added _into_ your package and |
| 85 | +can also be executed straight from the existing package. |
| 86 | + |
| 87 | +The idea behind adding the tests into the package is that you can execute the |
| 88 | +tests independently from building the package. That is also why we are shipping |
| 89 | +a `test` subcommand that takes as input an existing package and executes the |
| 90 | +tests: |
63 | 91 |
|
64 | | -- All the files are copied straight into the test folder (under |
65 | | - `info/tests/{index}/`) |
66 | | -- The script is turned into a `run_test.sh` or `run_test.bat` file |
67 | | -- The extra requirements are stored as a JSON file called |
68 | | - `test_time_dependencies.json` |
| 92 | +```bash |
| 93 | +rattler-build test --package-file ./xtensor-0.24.6-h60d57d3_0.tar.bz2 |
| 94 | +``` |
| 95 | + |
| 96 | +Running the above command will extract the package and create a clean |
| 97 | +environment where the package and dependencies are installed. Then the tests are |
| 98 | +executed in this newly-created environment. |
| 99 | + |
| 100 | +If you inspect the package contents, you would find the test files under |
| 101 | +`info/test/*`. |
| 102 | + |
| 103 | +## How tests are translated |
69 | 104 |
|
70 | | -For a Python import test: |
| 105 | +The `tests` section allows you to define test configurations for your package. |
| 106 | +Tests are serialized to `info/tests/tests.yaml` in the created package and read from there during test execution. |
71 | 107 |
|
72 | | -- A JSON file is created that is called `python_test.json` and stores the |
73 | | - imports to be tested and whether to execute `pip check` or not. This file is |
74 | | - placed under `info/tests/{index}/` |
| 108 | +When adding extra files to your tests: |
75 | 109 |
|
76 | | -For a downstream test: |
| 110 | +1. **During package creation** |
| 111 | + - Files are copied to `$PREFIX/etc/conda/test-files/{pkg_name}/{idx}` |
| 112 | + - `{idx}` is a sequential number assigned to each test |
| 113 | + - Files can come from both `source` (work directory) and `recipe` locations |
| 114 | +2. **During test execution** |
| 115 | + - Files are copied from `$PREFIX/etc/conda/test-files/{pkg_name}/{idx}` to a temporary directory |
| 116 | + - Tests run within this temporary directory |
| 117 | + - Use relative paths to access these files in your test commands |
77 | 118 |
|
78 | | -- A JSON file is created that is called `downstream_test.json` and stores the |
79 | | - downstream tests to be executed. This file is placed under |
80 | | - `info/tests/{index}/` |
| 119 | +This approach ensures test files are properly packaged and available during test execution. |
81 | 120 |
|
82 | 121 | ## Legacy tests |
83 | 122 |
|
|
0 commit comments