|
1390 | 1390 | "!pytest pytest_to_fail.py"
|
1391 | 1391 | ]
|
1392 | 1392 | },
|
| 1393 | + { |
| 1394 | + "cell_type": "markdown", |
| 1395 | + "id": "3657f5a4", |
| 1396 | + "metadata": {}, |
| 1397 | + "source": [ |
| 1398 | + "### Organize and Control Test Execution using pytest.mark" |
| 1399 | + ] |
| 1400 | + }, |
| 1401 | + { |
| 1402 | + "cell_type": "markdown", |
| 1403 | + "id": "dffdbfef", |
| 1404 | + "metadata": {}, |
| 1405 | + "source": [ |
| 1406 | + "pytest.mark lets you label test functions for conditional or selective execution based on specific needs. \n", |
| 1407 | + "\n", |
| 1408 | + "For instance, you can mark slow tests or tests involving integration with external services to run them separately or exclude them from regular test runs. This helps you organize and execute your tests more effectively." |
| 1409 | + ] |
| 1410 | + }, |
| 1411 | + { |
| 1412 | + "cell_type": "code", |
| 1413 | + "execution_count": 7, |
| 1414 | + "id": "12f48a85", |
| 1415 | + "metadata": {}, |
| 1416 | + "outputs": [ |
| 1417 | + { |
| 1418 | + "name": "stdout", |
| 1419 | + "output_type": "stream", |
| 1420 | + "text": [ |
| 1421 | + "Overwriting pytest_mark.py\n" |
| 1422 | + ] |
| 1423 | + } |
| 1424 | + ], |
| 1425 | + "source": [ |
| 1426 | + "%%writefile pytest_mark.py\n", |
| 1427 | + "import pytest\n", |
| 1428 | + "import time\n", |
| 1429 | + "\n", |
| 1430 | + "\n", |
| 1431 | + "@pytest.mark.slow\n", |
| 1432 | + "def test_long_running_function():\n", |
| 1433 | + " # Test that takes a long time to complete\n", |
| 1434 | + " time.sleep(5)\n", |
| 1435 | + "\n", |
| 1436 | + "\n", |
| 1437 | + "@pytest.mark.db\n", |
| 1438 | + "def test_database_interaction():\n", |
| 1439 | + " # Test that requires a database connection\n", |
| 1440 | + " pass\n", |
| 1441 | + "\n", |
| 1442 | + "\n", |
| 1443 | + "def test_function_1():\n", |
| 1444 | + " pass\n", |
| 1445 | + "\n", |
| 1446 | + "\n", |
| 1447 | + "def test_function_2():\n", |
| 1448 | + " pass" |
| 1449 | + ] |
| 1450 | + }, |
| 1451 | + { |
| 1452 | + "cell_type": "markdown", |
| 1453 | + "id": "155aa47c", |
| 1454 | + "metadata": {}, |
| 1455 | + "source": [ |
| 1456 | + "Run only slow tests:\n", |
| 1457 | + "\n", |
| 1458 | + "```bash\n", |
| 1459 | + "$ pytest pytest_mark.py -m slow\n", |
| 1460 | + "```" |
| 1461 | + ] |
| 1462 | + }, |
| 1463 | + { |
| 1464 | + "cell_type": "code", |
| 1465 | + "execution_count": 10, |
| 1466 | + "id": "244b7a98", |
| 1467 | + "metadata": { |
| 1468 | + "tags": [ |
| 1469 | + "remove-input" |
| 1470 | + ] |
| 1471 | + }, |
| 1472 | + "outputs": [ |
| 1473 | + { |
| 1474 | + "name": "stdout", |
| 1475 | + "output_type": "stream", |
| 1476 | + "text": [ |
| 1477 | + "\u001b[1m============================= test session starts ==============================\u001b[0m\n", |
| 1478 | + "platform darwin -- Python 3.11.2, pytest-7.4.3, pluggy-1.3.0\n", |
| 1479 | + "rootdir: /Users/khuyentran/book/Efficient_Python_tricks_and_tools_for_data_scientists/Chapter5\n", |
| 1480 | + "plugins: dvc-3.28.0, hydra-core-1.3.2, typeguard-4.1.5, anyio-4.2.0, hypothesis-6.88.4\n", |
| 1481 | + "collected 4 items / 3 deselected / 1 selected \u001b[0m\n", |
| 1482 | + "\n", |
| 1483 | + "pytest_mark.py \u001b[32m.\u001b[0m\u001b[33m [100%]\u001b[0m\n", |
| 1484 | + "\n", |
| 1485 | + "\u001b[33m================= \u001b[32m1 passed\u001b[0m, \u001b[33m\u001b[1m3 deselected\u001b[0m, \u001b[33m\u001b[1m2 warnings\u001b[0m\u001b[33m in 5.02s\u001b[0m\u001b[33m ==================\u001b[0m\n", |
| 1486 | + "\u001b[0m" |
| 1487 | + ] |
| 1488 | + } |
| 1489 | + ], |
| 1490 | + "source": [ |
| 1491 | + "!pytest pytest_mark.py -m slow --disable-pytest-warnings" |
| 1492 | + ] |
| 1493 | + }, |
| 1494 | + { |
| 1495 | + "cell_type": "markdown", |
| 1496 | + "id": "a71a7e21", |
| 1497 | + "metadata": {}, |
| 1498 | + "source": [ |
| 1499 | + "Skip slow tests:\n", |
| 1500 | + "\n", |
| 1501 | + "```bash\n", |
| 1502 | + "$ pytest pytest_mark.py -m \"not slow\"\n", |
| 1503 | + "```" |
| 1504 | + ] |
| 1505 | + }, |
| 1506 | + { |
| 1507 | + "cell_type": "code", |
| 1508 | + "execution_count": 11, |
| 1509 | + "id": "c5d03906", |
| 1510 | + "metadata": { |
| 1511 | + "tags": [ |
| 1512 | + "remove-input" |
| 1513 | + ] |
| 1514 | + }, |
| 1515 | + "outputs": [ |
| 1516 | + { |
| 1517 | + "name": "stdout", |
| 1518 | + "output_type": "stream", |
| 1519 | + "text": [ |
| 1520 | + "\u001b[1m============================= test session starts ==============================\u001b[0m\n", |
| 1521 | + "platform darwin -- Python 3.11.2, pytest-7.4.3, pluggy-1.3.0\n", |
| 1522 | + "rootdir: /Users/khuyentran/book/Efficient_Python_tricks_and_tools_for_data_scientists/Chapter5\n", |
| 1523 | + "plugins: dvc-3.28.0, hydra-core-1.3.2, typeguard-4.1.5, anyio-4.2.0, hypothesis-6.88.4\n", |
| 1524 | + "collected 4 items / 1 deselected / 3 selected \u001b[0m\n", |
| 1525 | + "\n", |
| 1526 | + "pytest_mark.py \u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[33m [100%]\u001b[0m\n", |
| 1527 | + "\n", |
| 1528 | + "\u001b[33m================= \u001b[32m3 passed\u001b[0m, \u001b[33m\u001b[1m1 deselected\u001b[0m, \u001b[33m\u001b[1m2 warnings\u001b[0m\u001b[33m in 0.01s\u001b[0m\u001b[33m ==================\u001b[0m\n", |
| 1529 | + "\u001b[0m" |
| 1530 | + ] |
| 1531 | + } |
| 1532 | + ], |
| 1533 | + "source": [ |
| 1534 | + "!pytest pytest_mark.py -m \"not slow\" --disable-pytest-warnings" |
| 1535 | + ] |
| 1536 | + }, |
| 1537 | + { |
| 1538 | + "cell_type": "markdown", |
| 1539 | + "id": "20d60730", |
| 1540 | + "metadata": {}, |
| 1541 | + "source": [ |
| 1542 | + "Skip database tests:\n", |
| 1543 | + "\n", |
| 1544 | + "```bash\n", |
| 1545 | + "$ pytest pytest_mark.py -m \"not db\" \n", |
| 1546 | + "```" |
| 1547 | + ] |
| 1548 | + }, |
| 1549 | + { |
| 1550 | + "cell_type": "code", |
| 1551 | + "execution_count": 12, |
| 1552 | + "id": "2ea78f1b", |
| 1553 | + "metadata": { |
| 1554 | + "tags": [ |
| 1555 | + "remove-input" |
| 1556 | + ] |
| 1557 | + }, |
| 1558 | + "outputs": [ |
| 1559 | + { |
| 1560 | + "name": "stdout", |
| 1561 | + "output_type": "stream", |
| 1562 | + "text": [ |
| 1563 | + "\u001b[1m============================= test session starts ==============================\u001b[0m\n", |
| 1564 | + "platform darwin -- Python 3.11.2, pytest-7.4.3, pluggy-1.3.0\n", |
| 1565 | + "rootdir: /Users/khuyentran/book/Efficient_Python_tricks_and_tools_for_data_scientists/Chapter5\n", |
| 1566 | + "plugins: dvc-3.28.0, hydra-core-1.3.2, typeguard-4.1.5, anyio-4.2.0, hypothesis-6.88.4\n", |
| 1567 | + "collected 4 items / 1 deselected / 3 selected \u001b[0m\n", |
| 1568 | + "\n", |
| 1569 | + "pytest_mark.py \u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[32m.\u001b[0m\u001b[33m [100%]\u001b[0m\n", |
| 1570 | + "\n", |
| 1571 | + "\u001b[33m================= \u001b[32m3 passed\u001b[0m, \u001b[33m\u001b[1m1 deselected\u001b[0m, \u001b[33m\u001b[1m2 warnings\u001b[0m\u001b[33m in 5.02s\u001b[0m\u001b[33m ==================\u001b[0m\n", |
| 1572 | + "\u001b[0m" |
| 1573 | + ] |
| 1574 | + } |
| 1575 | + ], |
| 1576 | + "source": [ |
| 1577 | + "!pytest pytest_mark.py -m \"not db\" --disable-pytest-warnings" |
| 1578 | + ] |
| 1579 | + }, |
1393 | 1580 | {
|
1394 | 1581 | "attachments": {},
|
1395 | 1582 | "cell_type": "markdown",
|
|
0 commit comments