ptest.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. .. SPDX-License-Identifier: CC-BY-SA-2.0-UK
  2. ***************************
  3. Testing Packages With ptest
  4. ***************************
  5. A Package Test (ptest) runs tests against packages built by the
  6. OpenEmbedded build system on the target machine. A ptest contains at
  7. least two items: the actual test, and a shell script (``run-ptest``)
  8. that starts the test. The shell script that starts the test must not
  9. contain the actual test --- the script only starts the test. On the other
  10. hand, the test can be anything from a simple shell script that runs a
  11. binary and checks the output to an elaborate system of test binaries and
  12. data files.
  13. The test generates output in the format used by Automake::
  14. result: testname
  15. where the result can be ``PASS``, ``FAIL``, or ``SKIP``, and
  16. the testname can be any identifying string.
  17. For a list of Yocto Project recipes that are already enabled with ptest,
  18. see the :yocto_wiki:`Ptest </Ptest>` wiki page.
  19. .. note::
  20. A recipe is "ptest-enabled" if it inherits the :ref:`ref-classes-ptest`
  21. class.
  22. Adding ptest to Your Build
  23. ==========================
  24. To add package testing to your build, add the :term:`DISTRO_FEATURES` and
  25. :term:`EXTRA_IMAGE_FEATURES` variables to your ``local.conf`` file, which
  26. is found in the :term:`Build Directory`::
  27. DISTRO_FEATURES:append = " ptest"
  28. EXTRA_IMAGE_FEATURES += "ptest-pkgs"
  29. Once your build is complete, the ptest files are installed into the
  30. ``/usr/lib/package/ptest`` directory within the image, where ``package``
  31. is the name of the package.
  32. Running ptest
  33. =============
  34. The ``ptest-runner`` package installs a shell script that loops through
  35. all installed ptest test suites and runs them in sequence. Consequently,
  36. you might want to add this package to your image.
  37. Getting Your Package Ready
  38. ==========================
  39. In order to enable a recipe to run installed ptests on target hardware,
  40. you need to prepare the recipes that build the packages you want to
  41. test. Here is what you have to do for each recipe:
  42. - *Be sure the recipe inherits the* :ref:`ref-classes-ptest` *class:*
  43. Include the following line in each recipe::
  44. inherit ptest
  45. .. note::
  46. Classes for common frameworks already exist in :term:`OpenEmbedded-Core
  47. (OE-Core)`, such as:
  48. - :oe_git:`go-ptest </openembedded-core/tree/meta/classes-recipe/go-ptest.bbclass>`
  49. - :ref:`ref-classes-ptest-cargo`
  50. - :ref:`ref-classes-ptest-gnome`
  51. - :oe_git:`ptest-perl </openembedded-core/tree/meta/classes-recipe/ptest-perl.bbclass>`
  52. - :oe_git:`ptest-python-pytest </openembedded-core/tree/meta/classes-recipe/ptest-python-pytest.bbclass>`
  53. Inheriting these classes with the ``inherit`` keyword in your recipe will
  54. make the next steps automatic.
  55. - *Create run-ptest:* This script starts your test. Locate the
  56. script where you will refer to it using
  57. :term:`SRC_URI`. Here is an
  58. example that starts a test for ``dbus``::
  59. #!/bin/sh
  60. cd test
  61. make -k runtest-TESTS
  62. - *Ensure dependencies are met:* If the test adds build or runtime
  63. dependencies that normally do not exist for the package (such as
  64. requiring "make" to run the test suite), use the
  65. :term:`DEPENDS` and
  66. :term:`RDEPENDS` variables in
  67. your recipe in order for the package to meet the dependencies. Here
  68. is an example where the package has a runtime dependency on "make"::
  69. RDEPENDS:${PN}-ptest += "make"
  70. - *Add a function to build the test suite:* Not many packages support
  71. cross-compilation of their test suites. Consequently, you usually
  72. need to add a cross-compilation function to the package.
  73. Many packages based on Automake compile and run the test suite by
  74. using a single command such as ``make check``. However, the host
  75. ``make check`` builds and runs on the same computer, while
  76. cross-compiling requires that the package is built on the host but
  77. executed for the target architecture (though often, as in the case
  78. for ptest, the execution occurs on the host). The built version of
  79. Automake that ships with the Yocto Project includes a patch that
  80. separates building and execution. Consequently, packages that use the
  81. unaltered, patched version of ``make check`` automatically
  82. cross-compiles.
  83. Regardless, you still must add a ``do_compile_ptest`` function to
  84. build the test suite. Add a function similar to the following to your
  85. recipe::
  86. do_compile_ptest() {
  87. oe_runmake buildtest-TESTS
  88. }
  89. - *Ensure special configurations are set:* If the package requires
  90. special configurations prior to compiling the test code, you must
  91. insert a ``do_configure_ptest`` function into the recipe.
  92. - *Install the test suite:* The :ref:`ref-classes-ptest` class
  93. automatically copies the file ``run-ptest`` to the target and then runs make
  94. ``install-ptest`` to run the tests. If this is not enough, you need
  95. to create a ``do_install_ptest`` function and make sure it gets
  96. called after the "make install-ptest" completes.