|
642 | 642 | {
|
643 | 643 | "source": [
|
644 | 644 | "```python\n",
|
645 |
| - "Code(\"greetings/greetings/test/test_greeter.py\")\n", |
646 | 645 | "import yaml\n",
|
647 | 646 | "import os\n",
|
648 | 647 | "from ..greeter import greet\n",
|
|
696 | 695 | "metadata": {}
|
697 | 696 | },
|
698 | 697 | {
|
699 |
| - "cell_type": "code", |
700 |
| - "execution_count": 17, |
| 698 | + "source": [ |
| 699 | + "We can now run `pytest`" |
| 700 | + ], |
| 701 | + "cell_type": "markdown", |
701 | 702 | "metadata": {
|
702 | 703 | "collapsed": false
|
703 |
| - }, |
704 |
| - "outputs": [ |
705 |
| - { |
706 |
| - "name": "stdout", |
707 |
| - "output_type": "stream", |
708 |
| - "text": [ |
709 |
| - "============================= test session starts ==============================\n", |
710 |
| - "platform linux -- Python 3.7.3, pytest-5.1.1, py-1.8.0, pluggy-0.12.0\n", |
711 |
| - "rootdir: /home/dvd/Documents/Work/RSDG/Projects/Teaching/rsd-engineeringcourse/ch04packaging\n", |
712 |
| - "collected 1 item\n", |
713 |
| - "\n", |
714 |
| - "greetings/greetings/test/test_greeter.py F [100%]\n", |
715 |
| - "\n", |
716 |
| - "=================================== FAILURES ===================================\n", |
717 |
| - "_________________________________ test_greeter _________________________________\n", |
718 |
| - "\n", |
719 |
| - " def test_greeter():\n", |
720 |
| - " with open(os.path.join(os.path.dirname(__file__),\n", |
721 |
| - " 'fixtures',\n", |
722 |
| - " 'samples.yaml')) as fixtures_file:\n", |
723 |
| - " fixtures = yaml.safe_load(fixtures_file)\n", |
724 |
| - " for fixture in fixtures:\n", |
725 |
| - " answer = fixture.pop('answer')\n", |
726 |
| - "> assert greet(**fixture) == answer\n", |
727 |
| - "E AssertionError: assert 'How do you d...aham Chapman.' == 'How do you d...aahm Chapman.'\n", |
728 |
| - "E - How do you do, Graham Chapman.\n", |
729 |
| - "E ? -\n", |
730 |
| - "E + How do you do, Graahm Chapman.\n", |
731 |
| - "E ? +\n", |
732 |
| - "\n", |
733 |
| - "greetings/greetings/test/test_greeter.py:12: AssertionError\n", |
734 |
| - "============================== 1 failed in 0.08s ===============================\n" |
735 |
| - ] |
736 |
| - } |
737 |
| - ], |
| 704 | + } |
| 705 | + }, |
| 706 | + { |
738 | 707 | "source": [
|
739 |
| - "%%bash --no-raise-error\n", |
740 |
| - "pytest" |
741 |
| - ] |
| 708 | + "```bash\n", |
| 709 | + "============================= test session starts ==============================\n", |
| 710 | + "platform linux -- Python 3.7.3, pytest-5.1.1, py-1.8.0, pluggy-0.12.0\n", |
| 711 | + "rootdir: /home/dvd/Documents/Work/RSDG/Projects/Teaching/rsd-engineeringcourse/ch04packaging\n", |
| 712 | + "collected 1 item\n", |
| 713 | + "\n", |
| 714 | + "greetings/greetings/test/test_greeter.py F [100%]\n", |
| 715 | + "\n", |
| 716 | + "=================================== FAILURES ===================================\n", |
| 717 | + "_________________________________ test_greeter _________________________________\n", |
| 718 | + "\n", |
| 719 | + " def test_greeter():\n", |
| 720 | + " with open(os.path.join(os.path.dirname(__file__),\n", |
| 721 | + " 'fixtures',\n", |
| 722 | + " 'samples.yaml')) as fixtures_file:\n", |
| 723 | + " fixtures = yaml.safe_load(fixtures_file)\n", |
| 724 | + " for fixture in fixtures:\n", |
| 725 | + " answer = fixture.pop('answer')\n", |
| 726 | + "> assert greet(**fixture) == answer\n", |
| 727 | + "E AssertionError: assert 'How do you d...aham Chapman.' == 'How do you d...aahm Chapman.'\n", |
| 728 | + "E - How do you do, Graham Chapman.\n", |
| 729 | + "E ? -\n", |
| 730 | + "E + How do you do, Graahm Chapman.\n", |
| 731 | + "E ? +\n", |
| 732 | + "\n", |
| 733 | + "greetings/greetings/test/test_greeter.py:12: AssertionError\n", |
| 734 | + "============================== 1 failed in 0.08s ===============================\n", |
| 735 | + "```" |
| 736 | + ], |
| 737 | + "cell_type": "markdown", |
| 738 | + "metadata": {} |
742 | 739 | },
|
743 | 740 | {
|
744 | 741 | "cell_type": "markdown",
|
745 | 742 | "metadata": {},
|
746 | 743 | "source": [
|
747 |
| - "However, this hasn't told us that also the third test is wrong! A better aproach is to parametrize the test as follows:" |
| 744 | + "However, this hasn't told us that also the third test is wrong too! A better aproach is to parametrize the testfile `greetings/greetings/test/test_greeter.py` as follows:" |
748 | 745 | ]
|
749 | 746 | },
|
750 | 747 | {
|
751 |
| - "cell_type": "code", |
752 |
| - "execution_count": 18, |
753 |
| - "metadata": {}, |
754 |
| - "outputs": [ |
755 |
| - { |
756 |
| - "name": "stdout", |
757 |
| - "output_type": "stream", |
758 |
| - "text": [ |
759 |
| - "Overwriting greetings/greetings/test/test_greeter.py\n" |
760 |
| - ] |
761 |
| - } |
762 |
| - ], |
763 | 748 | "source": [
|
764 |
| - "%%writefile greetings/greetings/test/test_greeter.py\n", |
| 749 | + "```python\n", |
765 | 750 | "import yaml\n",
|
766 | 751 | "import os\n",
|
767 | 752 | "import pytest\n",
|
|
777 | 762 | "@pytest.mark.parametrize(\"fixture\", read_fixture())\n",
|
778 | 763 | "def test_greeter(fixture):\n",
|
779 | 764 | " answer = fixture.pop('answer')\n",
|
780 |
| - " assert greet(**fixture) == answer\n" |
781 |
| - ] |
| 765 | + " assert greet(**fixture) == answer\n", |
| 766 | + "```" |
| 767 | + ], |
| 768 | + "cell_type": "markdown", |
| 769 | + "metadata": {} |
782 | 770 | },
|
783 | 771 | {
|
784 | 772 | "cell_type": "markdown",
|
|
788 | 776 | ]
|
789 | 777 | },
|
790 | 778 | {
|
791 |
| - "cell_type": "code", |
792 |
| - "execution_count": 19, |
793 |
| - "metadata": {}, |
794 |
| - "outputs": [ |
795 |
| - { |
796 |
| - "name": "stdout", |
797 |
| - "output_type": "stream", |
798 |
| - "text": [ |
799 |
| - "============================= test session starts ==============================\n", |
800 |
| - "platform linux -- Python 3.7.3, pytest-5.1.1, py-1.8.0, pluggy-0.12.0\n", |
801 |
| - "rootdir: /home/dvd/Documents/Work/RSDG/Projects/Teaching/rsd-engineeringcourse/ch04packaging\n", |
802 |
| - "collected 3 items\n", |
803 |
| - "\n", |
804 |
| - "greetings/greetings/test/test_greeter.py .FF [100%]\n", |
805 |
| - "\n", |
806 |
| - "=================================== FAILURES ===================================\n", |
807 |
| - "____________________________ test_greeter[fixture1] ____________________________\n", |
808 |
| - "\n", |
809 |
| - "fixture = {'family': 'Chapman', 'personal': 'Graham', 'polite': True}\n", |
810 |
| - "\n", |
811 |
| - " @pytest.mark.parametrize(\"fixture\", read_fixture())\n", |
812 |
| - " def test_greeter(fixture):\n", |
813 |
| - " answer = fixture.pop('answer')\n", |
814 |
| - "> assert greet(**fixture) == answer\n", |
815 |
| - "E AssertionError: assert 'How do you d...aham Chapman.' == 'How do you d...aahm Chapman.'\n", |
816 |
| - "E - How do you do, Graham Chapman.\n", |
817 |
| - "E ? -\n", |
818 |
| - "E + How do you do, Graahm Chapman.\n", |
819 |
| - "E ? +\n", |
820 |
| - "\n", |
821 |
| - "greetings/greetings/test/test_greeter.py:16: AssertionError\n", |
822 |
| - "____________________________ test_greeter[fixture2] ____________________________\n", |
823 |
| - "\n", |
824 |
| - "fixture = {'family': 'Palin', 'personal': 'Michael', 'title': 'CBE'}\n", |
825 |
| - "\n", |
826 |
| - " @pytest.mark.parametrize(\"fixture\", read_fixture())\n", |
827 |
| - " def test_greeter(fixture):\n", |
828 |
| - " answer = fixture.pop('answer')\n", |
829 |
| - "> assert greet(**fixture) == answer\n", |
830 |
| - "E AssertionError: assert 'Hey, CBE Michael Palin.' == 'Hey, CBE Mike Palin.'\n", |
831 |
| - "E - Hey, CBE Michael Palin.\n", |
832 |
| - "E ? ^^^ -\n", |
833 |
| - "E + Hey, CBE Mike Palin.\n", |
834 |
| - "E ? ^\n", |
835 |
| - "\n", |
836 |
| - "greetings/greetings/test/test_greeter.py:16: AssertionError\n", |
837 |
| - "========================= 2 failed, 1 passed in 0.09s ==========================\n" |
838 |
| - ] |
839 |
| - } |
840 |
| - ], |
841 | 779 | "source": [
|
842 |
| - "%%bash --no-raise-error\n", |
843 |
| - "pytest" |
844 |
| - ] |
| 780 | + "```bash\n", |
| 781 | + "============================= test session starts ==============================\n", |
| 782 | + "platform linux -- Python 3.7.3, pytest-5.1.1, py-1.8.0, pluggy-0.12.0\n", |
| 783 | + "rootdir: /home/dvd/Documents/Work/RSDG/Projects/Teaching/rsd-engineeringcourse/ch04packaging\n", |
| 784 | + "collected 3 items\n", |
| 785 | + "\n", |
| 786 | + "greetings/greetings/test/test_greeter.py .FF [100%]\n", |
| 787 | + "\n", |
| 788 | + "=================================== FAILURES ===================================\n", |
| 789 | + "____________________________ test_greeter[fixture1] ____________________________\n", |
| 790 | + "\n", |
| 791 | + "fixture = {'family': 'Chapman', 'personal': 'Graham', 'polite': True}\n", |
| 792 | + "\n", |
| 793 | + " @pytest.mark.parametrize(\"fixture\", read_fixture())\n", |
| 794 | + " def test_greeter(fixture):\n", |
| 795 | + " answer = fixture.pop('answer')\n", |
| 796 | + "> assert greet(**fixture) == answer\n", |
| 797 | + "E AssertionError: assert 'How do you d...aham Chapman.' == 'How do you d...aahm Chapman.'\n", |
| 798 | + "E - How do you do, Graham Chapman.\n", |
| 799 | + "E ? -\n", |
| 800 | + "E + How do you do, Graahm Chapman.\n", |
| 801 | + "E ? +\n", |
| 802 | + "\n", |
| 803 | + "greetings/greetings/test/test_greeter.py:16: AssertionError\n", |
| 804 | + "____________________________ test_greeter[fixture2] ____________________________\n", |
| 805 | + "\n", |
| 806 | + "fixture = {'family': 'Palin', 'personal': 'Michael', 'title': 'CBE'}\n", |
| 807 | + "\n", |
| 808 | + " @pytest.mark.parametrize(\"fixture\", read_fixture())\n", |
| 809 | + " def test_greeter(fixture):\n", |
| 810 | + " answer = fixture.pop('answer')\n", |
| 811 | + "> assert greet(**fixture) == answer\n", |
| 812 | + "E AssertionError: assert 'Hey, CBE Michael Palin.' == 'Hey, CBE Mike Palin.'\n", |
| 813 | + "E - Hey, CBE Michael Palin.\n", |
| 814 | + "E ? ^^^ -\n", |
| 815 | + "E + Hey, CBE Mike Palin.\n", |
| 816 | + "E ? ^\n", |
| 817 | + "\n", |
| 818 | + "greetings/greetings/test/test_greeter.py:16: AssertionError\n", |
| 819 | + "========================= 2 failed, 1 passed in 0.09s ==========================\n", |
| 820 | + "```" |
| 821 | + ], |
| 822 | + "cell_type": "markdown", |
| 823 | + "metadata": {} |
845 | 824 | },
|
846 | 825 | {
|
847 | 826 | "cell_type": "markdown",
|
848 | 827 | "metadata": {},
|
849 | 828 | "source": [
|
850 |
| - "We can also make pytest to check whether the docstrings are correct by adding the `--doctest-modules` flag:" |
| 829 | + "We can also make pytest to check whether the docstrings are correct by adding the `--doctest-modules` flag. We run `pytest --doctest-modules` and obtain the following output:" |
851 | 830 | ]
|
852 | 831 | },
|
853 | 832 | {
|
854 |
| - "cell_type": "code", |
855 |
| - "execution_count": 20, |
856 |
| - "metadata": {}, |
857 |
| - "outputs": [ |
858 |
| - { |
859 |
| - "name": "stdout", |
860 |
| - "output_type": "stream", |
861 |
| - "text": [ |
862 |
| - "============================= test session starts ==============================\n", |
863 |
| - "platform linux -- Python 3.7.3, pytest-5.1.1, py-1.8.0, pluggy-0.12.0\n", |
864 |
| - "rootdir: /home/dvd/Documents/Work/RSDG/Projects/Teaching/rsd-engineeringcourse/ch04packaging\n", |
865 |
| - "plugins: cov-2.8.1\n", |
866 |
| - "collected 4 items\n", |
867 |
| - "\n", |
868 |
| - "greetings/greetings/greeter.py F [ 25%]\n", |
869 |
| - "greetings/greetings/test/test_greeter.py .FF [100%]\n", |
870 |
| - "\n", |
871 |
| - "=================================== FAILURES ===================================\n", |
872 |
| - "______________________ [doctest] greetings.greeter.greet _______________________\n", |
873 |
| - "014 \n", |
874 |
| - "015 Returns\n", |
875 |
| - "016 -------\n", |
876 |
| - "017 string\n", |
877 |
| - "018 An appropriate greeting\n", |
878 |
| - "019 \n", |
879 |
| - "020 Examples\n", |
880 |
| - "021 --------\n", |
881 |
| - "022 >>> from greetings.greeter import greet\n", |
882 |
| - "023 >>> greet(\"Terry\", \"Jones\")\n", |
883 |
| - "Expected:\n", |
884 |
| - " 'Hey, Terry Jones.\n", |
885 |
| - "Got:\n", |
886 |
| - " 'Hey, Terry Jones.'\n", |
887 |
| - "\n", |
888 |
| - "/home/dvd/Documents/Work/RSDG/Projects/Teaching/rsd-engineeringcourse/ch04packaging/greetings/greetings/greeter.py:23: DocTestFailure\n", |
889 |
| - "____________________________ test_greeter[fixture1] ____________________________\n", |
890 |
| - "\n", |
891 |
| - "fixture = {'family': 'Chapman', 'personal': 'Graham', 'polite': True}\n", |
892 |
| - "\n", |
893 |
| - " @pytest.mark.parametrize(\"fixture\", read_fixture())\n", |
894 |
| - " def test_greeter(fixture):\n", |
895 |
| - " answer = fixture.pop('answer')\n", |
896 |
| - "> assert greet(**fixture) == answer\n", |
897 |
| - "E AssertionError: assert 'How do you d...aham Chapman.' == 'How do you d...aahm Chapman.'\n", |
898 |
| - "E - How do you do, Graham Chapman.\n", |
899 |
| - "E ? -\n", |
900 |
| - "E + How do you do, Graahm Chapman.\n", |
901 |
| - "E ? +\n", |
902 |
| - "\n", |
903 |
| - "greetings/greetings/test/test_greeter.py:16: AssertionError\n", |
904 |
| - "____________________________ test_greeter[fixture2] ____________________________\n", |
905 |
| - "\n", |
906 |
| - "fixture = {'family': 'Palin', 'personal': 'Michael', 'title': 'CBE'}\n", |
907 |
| - "\n", |
908 |
| - " @pytest.mark.parametrize(\"fixture\", read_fixture())\n", |
909 |
| - " def test_greeter(fixture):\n", |
910 |
| - " answer = fixture.pop('answer')\n", |
911 |
| - "> assert greet(**fixture) == answer\n", |
912 |
| - "E AssertionError: assert 'Hey, CBE Michael Palin.' == 'Hey, CBE Mike Palin.'\n", |
913 |
| - "E - Hey, CBE Michael Palin.\n", |
914 |
| - "E ? ^^^ -\n", |
915 |
| - "E + Hey, CBE Mike Palin.\n", |
916 |
| - "E ? ^\n", |
917 |
| - "\n", |
918 |
| - "greetings/greetings/test/test_greeter.py:16: AssertionError\n", |
919 |
| - "========================= 3 failed, 1 passed in 0.09s ==========================\n" |
920 |
| - ] |
921 |
| - } |
922 |
| - ], |
923 | 833 | "source": [
|
924 |
| - "%%bash --no-raise-error\n", |
925 |
| - "pytest --doctest-modules" |
926 |
| - ] |
| 834 | + "```bash\n", |
| 835 | + "============================= test session starts ==============================\n", |
| 836 | + "platform linux -- Python 3.7.3, pytest-5.1.1, py-1.8.0, pluggy-0.12.0\n", |
| 837 | + "rootdir: /home/dvd/Documents/Work/RSDG/Projects/Teaching/rsd-engineeringcourse/ch04packaging\n", |
| 838 | + "plugins: cov-2.8.1\n", |
| 839 | + "collected 4 items\n", |
| 840 | + "\n", |
| 841 | + "greetings/greetings/greeter.py F [ 25%]\n", |
| 842 | + "greetings/greetings/test/test_greeter.py .FF [100%]\n", |
| 843 | + "\n", |
| 844 | + "=================================== FAILURES ===================================\n", |
| 845 | + "______________________ [doctest] greetings.greeter.greet _______________________\n", |
| 846 | + "014 \n", |
| 847 | + "015 Returns\n", |
| 848 | + "016 -------\n", |
| 849 | + "017 string\n", |
| 850 | + "018 An appropriate greeting\n", |
| 851 | + "019 \n", |
| 852 | + "020 Examples\n", |
| 853 | + "021 --------\n", |
| 854 | + "022 >>> from greetings.greeter import greet\n", |
| 855 | + "023 >>> greet(\"Terry\", \"Jones\")\n", |
| 856 | + "Expected:\n", |
| 857 | + " 'Hey, Terry Jones.\n", |
| 858 | + "Got:\n", |
| 859 | + " 'Hey, Terry Jones.'\n", |
| 860 | + "\n", |
| 861 | + "/home/dvd/Documents/Work/RSDG/Projects/Teaching/rsd-engineeringcourse/ch04packaging/greetings/greetings/greeter.py:23: DocTestFailure\n", |
| 862 | + "____________________________ test_greeter[fixture1] ____________________________\n", |
| 863 | + "\n", |
| 864 | + "fixture = {'family': 'Chapman', 'personal': 'Graham', 'polite': True}\n", |
| 865 | + "\n", |
| 866 | + " @pytest.mark.parametrize(\"fixture\", read_fixture())\n", |
| 867 | + " def test_greeter(fixture):\n", |
| 868 | + " answer = fixture.pop('answer')\n", |
| 869 | + "> assert greet(**fixture) == answer\n", |
| 870 | + "E AssertionError: assert 'How do you d...aham Chapman.' == 'How do you d...aahm Chapman.'\n", |
| 871 | + "E - How do you do, Graham Chapman.\n", |
| 872 | + "E ? -\n", |
| 873 | + "E + How do you do, Graahm Chapman.\n", |
| 874 | + "E ? +\n", |
| 875 | + "\n", |
| 876 | + "greetings/greetings/test/test_greeter.py:16: AssertionError\n", |
| 877 | + "____________________________ test_greeter[fixture2] ____________________________\n", |
| 878 | + "\n", |
| 879 | + "fixture = {'family': 'Palin', 'personal': 'Michael', 'title': 'CBE'}\n", |
| 880 | + "\n", |
| 881 | + " @pytest.mark.parametrize(\"fixture\", read_fixture())\n", |
| 882 | + " def test_greeter(fixture):\n", |
| 883 | + " answer = fixture.pop('answer')\n", |
| 884 | + "> assert greet(**fixture) == answer\n", |
| 885 | + "E AssertionError: assert 'Hey, CBE Michael Palin.' == 'Hey, CBE Mike Palin.'\n", |
| 886 | + "E - Hey, CBE Michael Palin.\n", |
| 887 | + "E ? ^^^ -\n", |
| 888 | + "E + Hey, CBE Mike Palin.\n", |
| 889 | + "E ? ^\n", |
| 890 | + "\n", |
| 891 | + "greetings/greetings/test/test_greeter.py:16: AssertionError\n", |
| 892 | + "========================= 3 failed, 1 passed in 0.09s ==========================\n", |
| 893 | + "```" |
| 894 | + ], |
| 895 | + "cell_type": "markdown", |
| 896 | + "metadata": {} |
927 | 897 | },
|
928 | 898 | {
|
929 | 899 | "cell_type": "markdown",
|
|
0 commit comments