{"id":27152,"date":"2021-03-22T03:23:13","date_gmt":"2021-03-22T03:23:13","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=27152"},"modified":"2025-05-17T06:14:16","modified_gmt":"2025-05-17T06:14:16","slug":"difference-between-test-decorators-and-markers","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/difference-between-test-decorators-and-markers\/","title":{"rendered":"Difference between Test Decorators and Markers\u00a0"},"content":{"rendered":"<h1>Difference between Test Decorators and Markers<\/h1>\n<p>PyTest is a popular testing framework for Python that provides powerful features for writing and managing tests. Among these features, test decorators and markers play a crucial role in customizing and controlling test execution. Decorators modify test functions, while markers help categorize and selectively run tests.<\/p>\n<h2>Test Decorators in PyTest<\/h2>\n<p>Test decorators in PyTest are special Python functions that modify the behavior of test functions. They are typically used to skip tests, parameterize test cases, or set up fixtures. Decorators help enhance test functions without modifying their core logic.<\/p>\n<h2>Test Markers in PyTest<\/h2>\n<p>Test markers in PyTest are used to categorize tests and control their execution. They allow users to selectively run specific tests, group tests under different categories, and apply conditions for execution. Markers help in organizing large test suites efficiently.<\/p>\n<h2>Markers vs Decorators<\/h2>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\">\n<tbody>\n<tr>\n<th>Feature<\/th>\n<th>Test Decorators<\/th>\n<th>Test Markers<\/th>\n<\/tr>\n<tr>\n<td>Purpose<\/td>\n<td>Modifies the behavior of test functions<\/td>\n<td>Categorizes and controls test execution<\/td>\n<\/tr>\n<tr>\n<td>Usage<\/td>\n<td>Used for skipping tests, parameterizing, or using fixtures<\/td>\n<td>Used for tagging tests, setting conditions, and filtering tests<\/td>\n<\/tr>\n<tr>\n<td>Syntax<\/td>\n<td>Applied using <code class=\"\" data-line=\"\">@pytest...<\/code> before the test function<\/td>\n<td>Defined using <code class=\"\" data-line=\"\">@pytest.mark...<\/code> before the test function<\/td>\n<\/tr>\n<tr>\n<td>Effect<\/td>\n<td>Changes test execution behavior<\/td>\n<td>Determines which tests to run based on criteria<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>Example of Test Decorator<\/h2>\n<pre><code class=\"\" data-line=\"\">import pytest\n\n@pytest.mark.skip(reason=&quot;This test is temporarily disabled&quot;)\ndef test_example():\n    assert 1 + 1 == 2\n    <\/code><\/pre>\n<p>In this example, the <code class=\"\" data-line=\"\">@pytest.mark.skip<\/code> decorator is used to skip the test execution.<\/p>\n<h2>Example of Test Marker<\/h2>\n<pre><code class=\"\" data-line=\"\">import pytest\n\n@pytest.mark.slow\ndef test_slow_function():\n    import time\n    time.sleep(5)\n    assert True\n    <\/code><\/pre>\n<p>Here, <code class=\"\" data-line=\"\">@pytest.mark.slow<\/code> is a custom marker that can be used to categorize the test as a slow-running test.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Difference between Test Decorators and Markers PyTest is a popular testing framework for Python that provides powerful features for writing and managing tests. Among these features, test decorators and markers play a crucial role in customizing and controlling test execution. Decorators modify test functions, while markers help categorize and selectively run tests. Test Decorators in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[211],"tags":[],"class_list":["post-27152","post","type-post","status-publish","format-standard","hentry","category-python","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/27152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=27152"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/27152\/revisions"}],"predecessor-version":[{"id":27394,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/27152\/revisions\/27394"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=27152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=27152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=27152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}