rspec/rules/S5918/python/rule.adoc

75 lines
1.8 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
Test frameworks provide a mechanism to skip tests if their prerequisites are not met, by either calling dedicated methods (e.g: ``++unittest.TestCase.skipTest++``, ``++pytest.skip++``, ...) or using decorators (e.g: ``++unittest.skip++``, ``++pytest.mark.skip++``, ...)
Using a ``++return++`` statement instead will make the test succeed, even though no assertion has been performed. It is therefore better to flag the test as ``++skipped++`` in such situation.
This rule raises an issue when a ``++return++`` is performed conditionally at the beginning of a test method.
No issue will be raised if the ``++return++`` is unconditional as S1763 already raises an issue in such case.
The supported frameworks are ``++Pytest++`` and ``++Unittest++``.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
import unittest
class MyTest(unittest.TestCase):
def test_something(self):
if not external_resource_available():
return # Noncompliant
self.assertEqual(foo(), 42)
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,python]
2021-04-28 16:49:39 +02:00
----
import unittest
class MyTest(unittest.TestCase):
def test_something(self):
if not external_resource_available():
self.skipTest("prerequisite not met")
self.assertEqual(foo(), 42)
----
== Resources
2021-04-28 16:49:39 +02:00
2021-06-25 09:02:46 +02:00
* https://docs.pytest.org/en/latest/how-to/skipping.html[Pytest: skipping test functions]
2021-04-28 16:49:39 +02:00
* https://docs.python.org/3/library/unittest.html#skipping-tests-and-expected-failures[Unittest: skipping tests and expected failures]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Skip this test explicitly.
=== Highlighting
The return statement
'''
== Comments And Links
(visible only on this page)
=== is related to: S1763
endif::env-github,rspecator-view[]