Merge pull request #4558 from hyunchel/fix-incorrect-example-in-cache-doc
Update cache doc with correct example
This commit is contained in:
commit
b5cd43bc95
|
@ -0,0 +1 @@
|
||||||
|
Update cache documentation example to correctly show cache hit and miss.
|
|
@ -185,11 +185,14 @@ across pytest invocations::
|
||||||
import pytest
|
import pytest
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
def expensive_computation():
|
||||||
|
print("running expensive computation...")
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def mydata(request):
|
def mydata(request):
|
||||||
val = request.config.cache.get("example/value", None)
|
val = request.config.cache.get("example/value", None)
|
||||||
if val is None:
|
if val is None:
|
||||||
time.sleep(9*0.6) # expensive computation :)
|
expensive_computation()
|
||||||
val = 42
|
val = 42
|
||||||
request.config.cache.set("example/value", val)
|
request.config.cache.set("example/value", val)
|
||||||
return val
|
return val
|
||||||
|
@ -197,8 +200,7 @@ across pytest invocations::
|
||||||
def test_function(mydata):
|
def test_function(mydata):
|
||||||
assert mydata == 23
|
assert mydata == 23
|
||||||
|
|
||||||
If you run this command once, it will take a while because
|
If you run this command for the first time, you can see the print statement:
|
||||||
of the sleep:
|
|
||||||
|
|
||||||
.. code-block:: pytest
|
.. code-block:: pytest
|
||||||
|
|
||||||
|
@ -217,7 +219,7 @@ of the sleep:
|
||||||
1 failed in 0.12 seconds
|
1 failed in 0.12 seconds
|
||||||
|
|
||||||
If you run it a second time the value will be retrieved from
|
If you run it a second time the value will be retrieved from
|
||||||
the cache and this will be quick:
|
the cache and nothing will be printed:
|
||||||
|
|
||||||
.. code-block:: pytest
|
.. code-block:: pytest
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue