From patchwork Sun Oct 23 12:54:08 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bagas Sanjaya X-Patchwork-Id: 8016 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4242:0:0:0:0:0 with SMTP id s2csp1603987wrr; Sun, 23 Oct 2022 06:00:13 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4w2cmAnAPXdk/MJRfcBRzOslujyBtm5ZYZte/Wh+Nn35nSRFGQSzwLTqEP3FOikTspKL3G X-Received: by 2002:a05:6402:348f:b0:461:8a9d:2e37 with SMTP id v15-20020a056402348f00b004618a9d2e37mr5362617edc.150.1666530012963; Sun, 23 Oct 2022 06:00:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666530012; cv=none; d=google.com; s=arc-20160816; b=Nk1JjDHnV3qJrEBNPKEY15QtmgOwRYYtGlLWUk0SR2niF7Ccfn59b+m0G573lfRO/y 9QN4bw5nFyNwpm2lQP7f8wY2xAyTtTU6onkYLsGHe58iHwYz3pJfL1ISDt39+Z6bKnb3 quSKXnJEbacSExUGJMiaF53WwnlkmvH4dvI9WCpNYBCJIb801yOby21mi6YOAnW8lyZo 2KcMTavcBdyWoSsO79P5sw4yLSxQz2A22T/trkhoKYxL9MltxhwN1uV6kLQyIsAvUHLh nXBrPPTC55CXruKNpHp1nGLIOoqrdKIaxPGWTvpOQf50z2g3VxJkov3Nr45Y3KDXDtRn dgDA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Ixa9Q2USMZVhIzGh4MBP10w3G2Xm+tYD7e/S5JSlY6c=; b=aQr/mfJ44IphnvBOctlPkj411J7KEZnTUzknYQ1e8Tz/XRDDptLhz6YwZHB4CEGuj1 lR0Vs/vfA/3o1OP07sGGRKT9wentLS39HwucrM5nUwjDP+LnIaw4Q/K4lmTaGEn3vYID lL6tbectU8oks3L4X2qb2AdFsCQAI5vwfFKexm4jHIh42TTNQUERpmekCOIK3YogmtmA 7rrn/wj29LbqlHxX0RbQz83QzckDDB+5uUGmD7s5PYQc/LjQUW88lU/9QBa7iFGOruSU WKkpLbP2K811V8bBuZCneJ/MgadkfJQ9roGO5sRiyMqAw12nxeFufaMf+wM6DHZw7QiP iLAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=YZub1opv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nb3-20020a1709071c8300b007a7fc67c880si192868ejc.71.2022.10.23.05.59.49; Sun, 23 Oct 2022 06:00:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=YZub1opv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230333AbiJWMz0 (ORCPT + 99 others); Sun, 23 Oct 2022 08:55:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59790 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229973AbiJWMzW (ORCPT ); Sun, 23 Oct 2022 08:55:22 -0400 Received: from mail-pj1-x102c.google.com (mail-pj1-x102c.google.com [IPv6:2607:f8b0:4864:20::102c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D00D72656D; Sun, 23 Oct 2022 05:55:20 -0700 (PDT) Received: by mail-pj1-x102c.google.com with SMTP id t10-20020a17090a4e4a00b0020af4bcae10so6990185pjl.3; Sun, 23 Oct 2022 05:55:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Ixa9Q2USMZVhIzGh4MBP10w3G2Xm+tYD7e/S5JSlY6c=; b=YZub1opvBM/rm6EqBO8wWMRkXO0TGkK8l3eH2nAwSo85z0HdbKN71E4SYDEIyXbrlE ODmYJ0TA3mad6Mc1FkbGFLcTUH8nOiCi4+gjBG+Tm4cD8kQV+UOUPsATsDzD7E3d6Ace f2vRe3EKwaii8riit++oU1/kl6XgruIGEZjHeaklyBktySa2CF+v4ikF2s/Wi5swTJi2 phYRb8pWH3ka2AV+c61igG077f3KIlPfZBPbJ/TkI5cvxolGfvPhU09fuMNdW2TNJGSm V085xLxEY9sJOgCIfaFed+GORrJROv0/PCSuTlzYQzFn4E5X9U96th+7AvRqUF+eIijA NdUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Ixa9Q2USMZVhIzGh4MBP10w3G2Xm+tYD7e/S5JSlY6c=; b=qKjYLKBTfz7YJKflnBkapZbVKPpAWJ/CpU10i2DqOm2fZj/AJ1dG5JSxNPj8nCJ/28 2Dp/bhokX6hDWIaHftEvy652F9tJSCTvKuHjLSQrgvKcBbdmZ5HPDkQVJ0CUn6tnbKP3 K8jZ6rxbWGvPtY4bJfzJN72jGFkX631saiXlDYXXuZNWdRxtaCqBTF+WFmOHkdfgeBOn vsgyFmrI9bzD5lQsxSJ4DAu2KRxI6rlIcZuV4VeIValHxZnLg4kd+TFz6xHuOLW4XQhv uhU/2utqOIW21eu+FUEJ5rylrNUy9B1Q6Nke0O6t/TcfbyrExWX5xjjoZuk6zWyW114T F+yg== X-Gm-Message-State: ACrzQf1v6yrWCxfHhbxNPME1WG5vjn+Gla+wDbvE0reyh+p8U5oQsIG/ cE+Q9NJs5NE6+zEO4prpqtgRhGX71/N0Ew== X-Received: by 2002:a17:903:18b:b0:185:43e6:20df with SMTP id z11-20020a170903018b00b0018543e620dfmr27969785plg.4.1666529720134; Sun, 23 Oct 2022 05:55:20 -0700 (PDT) Received: from debian.. (subs02-180-214-232-1.three.co.id. [180.214.232.1]) by smtp.gmail.com with ESMTPSA id om10-20020a17090b3a8a00b00212daa68b7csm2670639pjb.44.2022.10.23.05.55.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Oct 2022 05:55:19 -0700 (PDT) From: Bagas Sanjaya To: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com Cc: Jonathan Corbet , Brendan Higgins , David Gow , Lukas Bulwahn , Khalid Masum , Sadiya Kazi , Bagas Sanjaya Subject: [PATCH 1/7] Documentation: kunit: rewrite "Writing Your First Test" section Date: Sun, 23 Oct 2022 19:54:08 +0700 Message-Id: <20221023125414.60961-2-bagasdotme@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221023125414.60961-1-bagasdotme@gmail.com> References: <20221023125414.60961-1-bagasdotme@gmail.com> MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=3749; i=bagasdotme@gmail.com; h=from:subject; bh=g91P+iZ/B5WmHtrZ4ClqKE+RwGXIgqSSfN3JfN7FHdA=; b=owGbwMvMwCH2bWenZ2ig32LG02pJDMmhljmeKW8/XWvk3//0R7jbIUUDdU8W3Yb1EwOalNd/2R98 qmN5RykLgxgHg6yYIsukRL6m07uMRC60r3WEmcPKBDKEgYtTACZyxZqR4Wa54K8HPI6unk3RCt4OEe 6lcsVr53wK/b8+PMRgbW/9MUaGs94H796Mu16y4EjEu0VTt9n9vel6geFYr8yTJbq5G98t4gcA X-Developer-Key: i=bagasdotme@gmail.com; a=openpgp; fpr=701B806FDCA5D3A58FFB8F7D7C276C64A5E44A1D X-Spam-Status: No, score=-0.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_SORBS_WEB,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747483374738419433?= X-GMAIL-MSGID: =?utf-8?q?1747483374738419433?= The "Writing Your First Test" section in "Getting Started" documentation have wordings that need to be tweaked for clarity and reducing redundancy. Rewrite the section. Signed-off-by: Bagas Sanjaya --- Documentation/dev-tools/kunit/start.rst | 34 +++++++++++++++---------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/Documentation/dev-tools/kunit/start.rst b/Documentation/dev-tools/kunit/start.rst index f4f504f1fb154f..590e25166efb0d 100644 --- a/Documentation/dev-tools/kunit/start.rst +++ b/Documentation/dev-tools/kunit/start.rst @@ -179,15 +179,19 @@ are built-in. Otherwise the module will need to be loaded. Writing Your First Test ======================= -In your kernel repository, let's add some code that we can test. +In this tutorial, you will learn how to write and test a simple driver +which performs addition of two integers. -1. Create a file ``drivers/misc/example.h``, which includes: +1. First, write the driver implementation. Follow the below steps. + + a. Create a new header file ``drivers/misc/example.h`` and add the + prototype for ``misc_example_add()``: .. code-block:: c int misc_example_add(int left, int right); -2. Create a file ``drivers/misc/example.c``, which includes: + b. Write the function implementation in ``drivers/misc/example.c``: .. code-block:: c @@ -200,22 +204,25 @@ In your kernel repository, let's add some code that we can test. return left + right; } -3. Add the following lines to ``drivers/misc/Kconfig``: + c. In order for the driver to be selected, add configuration entry to + ``drivers/misc/Kconfig``: .. code-block:: kconfig config MISC_EXAMPLE bool "My example" -4. Add the following lines to ``drivers/misc/Makefile``: + d. Last but not least, append the make goal to ``drivers/misc/Makefile`` + so that the driver can be built: .. code-block:: make obj-$(CONFIG_MISC_EXAMPLE) += example.o -Now we are ready to write the test cases. +2. Write the test suite that covers the driver functionality. Follow the + steps below: -1. Add the below test case in ``drivers/misc/example_test.c``: + a. Write the test in ``drivers/misc/example_test.c``: .. code-block:: c @@ -250,7 +257,7 @@ Now we are ready to write the test cases. }; kunit_test_suite(misc_example_test_suite); -2. Add the following lines to ``drivers/misc/Kconfig``: + b. Add configuration entry for the test suite to ``drivers/misc/Kconfig``: .. code-block:: kconfig @@ -259,26 +266,27 @@ Now we are ready to write the test cases. depends on MISC_EXAMPLE && KUNIT=y default KUNIT_ALL_TESTS -3. Add the following lines to ``drivers/misc/Makefile``: + c. Append make goal for the configuration to ``drivers/misc/Makefile``: .. code-block:: make obj-$(CONFIG_MISC_EXAMPLE_TEST) += example_test.o -4. Add the following lines to ``.kunit/.kunitconfig``: +3. In order to enable the driver and its test suite, append configuration + fragment to ``.kunit/.kunitconfig``: .. code-block:: none CONFIG_MISC_EXAMPLE=y CONFIG_MISC_EXAMPLE_TEST=y -5. Run the test: +4. Run the test: .. code-block:: bash ./tools/testing/kunit/kunit.py run -You should see the following failure: +You should see the following output: .. code-block:: none @@ -289,7 +297,7 @@ You should see the following failure: [16:08:57] This test never passes. ... -Congrats! You just wrote your first KUnit test. +Congrats! You have just written your first KUnit test. Next Steps ==========