/usr/lib/python3/dist-packages/autopilot/tests/functional/test_autopilot_functional.py is in python3-autopilot-tests 1.4+14.04.20140416-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 | # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Autopilot Functional Test Tool
# Copyright (C) 2012-2013 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from __future__ import absolute_import
import os
import os.path
import re
from tempfile import mktemp
from testtools import skipIf
from testtools.matchers import Contains, Equals, MatchesRegex, Not
from textwrap import dedent
from autopilot import platform
from autopilot.testcase import AutopilotTestCase
from autopilot.tests.functional import AutopilotRunTestBase, remove_if_exists
class AutopilotFunctionalTestsBase(AutopilotRunTestBase):
"""A collection of functional tests for autopilot."""
def run_autopilot_list(self, list_spec='tests', extra_args=[]):
"""Run 'autopilot list' in the specified base path.
This patches the environment to ensure that it's *this* version of
autopilot that's run.
returns a tuple containing: (exit_code, stdout, stderr)
"""
args_list = ["list"] + extra_args + [list_spec]
return self.run_autopilot(args_list)
def assertTestsInOutput(self, tests, output, total_title='tests'):
"""Asserts that 'tests' are all present in 'output'.
This assertion is intelligent enough to know that tests are not always
printed in alphabetical order.
'tests' can either be a list of test ids, or a list of tuples
containing (scenario_count, test_id), in the case of scenarios.
"""
if type(tests) is not list:
raise TypeError("tests must be a list, not %r" % type(tests))
if not isinstance(output, str):
raise TypeError("output must be a string, not %r" % type(output))
expected_heading = 'Loading tests from: %s\n\n' % self.base_path
expected_tests = []
expected_total = 0
for test in tests:
if type(test) == tuple:
expected_tests.append(' *%d %s' % test)
expected_total += test[0]
else:
expected_tests.append(' %s' % test)
expected_total += 1
expected_footer = ' %d total %s.' % (expected_total, total_title)
parts = output.split('\n')
observed_heading = '\n'.join(parts[:2]) + '\n'
observed_test_list = parts[2:-4]
observed_footer = parts[-2]
self.assertThat(expected_heading, Equals(observed_heading))
self.assertThat(
sorted(expected_tests),
Equals(sorted(observed_test_list))
)
self.assertThat(expected_footer, Equals(observed_footer))
def test_can_list_empty_test_dir(self):
"""Autopilot list must report 0 tests found with an empty test
module."""
code, output, error = self.run_autopilot_list()
self.assertThat(code, Equals(0))
self.assertThat(error, Equals(''))
self.assertTestsInOutput([], output)
def test_can_list_tests(self):
"""Autopilot must find tests in a file."""
self.create_test_file(
'test_simple.py', dedent("""\
from autopilot.testcase import AutopilotTestCase
class SimpleTest(AutopilotTestCase):
def test_simple(self):
pass
""")
)
# ideally these would be different tests, but I'm lazy:
valid_test_specs = [
'tests',
'tests.test_simple',
'tests.test_simple.SimpleTest',
'tests.test_simple.SimpleTest.test_simple',
]
for test_spec in valid_test_specs:
code, output, error = self.run_autopilot_list(test_spec)
self.assertThat(code, Equals(0))
self.assertThat(error, Equals(''))
self.assertTestsInOutput(
['tests.test_simple.SimpleTest.test_simple'], output)
def test_list_tests_with_import_error(self):
self.create_test_file(
'test_simple.py', dedent("""\
from autopilot.testcase import AutopilotTestCase
# create an import error:
import asdjkhdfjgsdhfjhsd
class SimpleTest(AutopilotTestCase):
def test_simple(self):
pass
""")
)
code, output, error = self.run_autopilot_list()
self.assertThat(code, Equals(0))
self.assertThat(error, Equals(''))
self.assertThat(
output,
MatchesRegex(
".*ImportError: No module named [']?asdjkhdfjgsdhfjhsd[']?.*",
re.DOTALL
)
)
def test_list_tests_with_syntax_error(self):
self.create_test_file(
'test_simple.py', dedent("""\
from autopilot.testcase import AutopilotTestCase
# create a syntax error:
..
class SimpleTest(AutopilotTestCase):
def test_simple(self):
pass
""")
)
code, output, error = self.run_autopilot_list()
expected_error = 'SyntaxError: invalid syntax'
self.assertThat(code, Equals(0))
self.assertThat(error, Equals(''))
self.assertThat(output, Contains(expected_error))
def test_list_nonexistent_test_returns_nonzero(self):
code, output, error = self.run_autopilot_list(list_spec='1234')
expected_msg = "could not import package 1234: No module"
expected_result = "0 total tests"
self.assertThat(code, Equals(1))
self.assertThat(output, Contains(expected_msg))
self.assertThat(output, Contains(expected_result))
def test_can_list_scenariod_tests(self):
"""Autopilot must show scenario counts next to tests that have
scenarios."""
self.create_test_file(
'test_simple.py', dedent("""\
from autopilot.testcase import AutopilotTestCase
class SimpleTest(AutopilotTestCase):
scenarios = [
('scenario one', {'key': 'value'}),
]
def test_simple(self):
pass
""")
)
expected_output = '''\
Loading tests from: %s
*1 tests.test_simple.SimpleTest.test_simple
1 total tests.
''' % self.base_path
code, output, error = self.run_autopilot_list()
self.assertThat(code, Equals(0))
self.assertThat(error, Equals(''))
self.assertThat(output, Equals(expected_output))
def test_can_list_scenariod_tests_with_multiple_scenarios(self):
"""Autopilot must show scenario counts next to tests that have
scenarios.
Tests multiple scenarios on a single test suite with multiple test
cases.
"""
self.create_test_file(
'test_simple.py', dedent("""\
from autopilot.testcase import AutopilotTestCase
class SimpleTest(AutopilotTestCase):
scenarios = [
('scenario one', {'key': 'value'}),
('scenario two', {'key': 'value2'}),
]
def test_simple(self):
pass
def test_simple_two(self):
pass
""")
)
code, output, error = self.run_autopilot_list()
self.assertThat(code, Equals(0))
self.assertThat(error, Equals(''))
self.assertTestsInOutput(
[
(2, 'tests.test_simple.SimpleTest.test_simple'),
(2, 'tests.test_simple.SimpleTest.test_simple_two'),
],
output
)
def test_can_list_invalid_scenarios(self):
"""Autopilot must ignore scenarios that are not lists."""
self.create_test_file(
'test_simple.py', dedent("""\
from autopilot.testcase import AutopilotTestCase
class SimpleTest(AutopilotTestCase):
scenarios = None
def test_simple(self):
pass
""")
)
code, output, error = self.run_autopilot_list()
self.assertThat(code, Equals(0))
self.assertThat(error, Equals(''))
self.assertTestsInOutput(
['tests.test_simple.SimpleTest.test_simple'], output)
def test_local_module_loaded_and_not_system_module(self):
module_path1 = self.create_empty_test_module()
module_path2 = self.create_empty_test_module()
self.base_path = module_path2
retcode, stdout, stderr = self.run_autopilot(
["run", "tests"],
pythonpath=module_path1,
use_script=True
)
self.assertThat(stdout, Contains(module_path2))
def test_can_list_just_suites(self):
"""Must only list available suites, not the contained tests."""
self.create_test_file(
'test_simple_suites.py', dedent("""\
from autopilot.testcase import AutopilotTestCase
class SimpleTest(AutopilotTestCase):
def test_simple(self):
pass
class AnotherSimpleTest(AutopilotTestCase):
def test_another_simple(self):
pass
def test_yet_another_simple(self):
pass
""")
)
code, output, error = self.run_autopilot_list(extra_args=['--suites'])
self.assertThat(code, Equals(0))
self.assertThat(error, Equals(''))
self.assertTestsInOutput(
['tests.test_simple_suites.SimpleTest',
'tests.test_simple_suites.AnotherSimpleTest'],
output, total_title='suites')
@skipIf(platform.model() != "Desktop", "Only suitable on Desktop (VidRec)")
def test_record_flag_works(self):
"""Must be able to record videos when the -r flag is present."""
# The sleep is to avoid the case where recordmydesktop does not create
# a file because it gets stopped before it's even started capturing
# anything.
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
from time import sleep
class SimpleTest(AutopilotTestCase):
def test_simple(self):
sleep(1)
self.fail()
""")
)
should_delete = not os.path.exists('/tmp/autopilot')
if should_delete:
self.addCleanup(remove_if_exists, "/tmp/autopilot")
else:
self.addCleanup(
remove_if_exists,
'/tmp/autopilot/tests.test_simple.SimpleTest.test_simple.ogv')
code, output, error = self.run_autopilot(["run", "-r", "tests"])
self.assertThat(code, Equals(1))
self.assertTrue(os.path.exists('/tmp/autopilot'))
self.assertTrue(os.path.exists(
'/tmp/autopilot/tests.test_simple.SimpleTest.test_simple.ogv'))
if should_delete:
self.addCleanup(remove_if_exists, "/tmp/autopilot")
@skipIf(platform.model() != "Desktop", "Only suitable on Desktop (VidRec)")
def test_record_dir_option_and_record_works(self):
"""Must be able to specify record directory flag and record."""
# The sleep is to avoid the case where recordmydesktop does not create
# a file because it gets stopped before it's even started capturing
# anything.
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
from time import sleep
class SimpleTest(AutopilotTestCase):
def test_simple(self):
sleep(1)
self.fail()
""")
)
video_dir = mktemp()
ap_dir = '/tmp/autopilot'
self.addCleanup(remove_if_exists, video_dir)
should_delete = not os.path.exists(ap_dir)
if should_delete:
self.addCleanup(remove_if_exists, ap_dir)
else:
self.addCleanup(
remove_if_exists,
'%s/tests.test_simple.SimpleTest.test_simple.ogv' % (ap_dir))
code, output, error = self.run_autopilot(
["run", "-r", "-rd", video_dir, "tests"])
self.assertThat(code, Equals(1))
self.assertTrue(os.path.exists(video_dir))
self.assertTrue(os.path.exists(
'%s/tests.test_simple.SimpleTest.test_simple.ogv' % (video_dir)))
self.assertFalse(
os.path.exists(
'%s/tests.test_simple.SimpleTest.test_simple.ogv' % (ap_dir)))
@skipIf(platform.model() != "Desktop", "Only suitable on Desktop (VidRec)")
def test_record_dir_option_works(self):
"""Must be able to specify record directory flag."""
# The sleep is to avoid the case where recordmydesktop does not create
# a file because it gets stopped before it's even started capturing
# anything.
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
from time import sleep
class SimpleTest(AutopilotTestCase):
def test_simple(self):
sleep(1)
self.fail()
""")
)
video_dir = mktemp()
self.addCleanup(remove_if_exists, video_dir)
code, output, error = self.run_autopilot(
["run", "-rd", video_dir, "tests"])
self.assertThat(code, Equals(1))
self.assertTrue(os.path.exists(video_dir))
self.assertTrue(
os.path.exists(
'%s/tests.test_simple.SimpleTest.test_simple.ogv' %
(video_dir)))
@skipIf(platform.model() != "Desktop", "Only suitable on Desktop (VidRec)")
def test_no_videos_saved_when_record_option_is_not_present(self):
"""Videos must not be saved if the '-r' option is not specified."""
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
from time import sleep
class SimpleTest(AutopilotTestCase):
def test_simple(self):
sleep(1)
self.fail()
""")
)
self.addCleanup(
remove_if_exists,
'/tmp/autopilot/tests.test_simple.SimpleTest.test_simple.ogv')
code, output, error = self.run_autopilot(["run", "tests"])
self.assertThat(code, Equals(1))
self.assertFalse(os.path.exists(
'/tmp/autopilot/tests.test_simple.SimpleTest.test_simple.ogv'))
@skipIf(platform.model() != "Desktop", "Only suitable on Desktop (VidRec)")
def test_no_videos_saved_for_skipped_test(self):
"""Videos must not be saved if the test has been skipped (not
failed).
"""
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
from time import sleep
class SimpleTest(AutopilotTestCase):
def test_simple(self):
sleep(1)
self.skip("Skipping Test")
""")
)
video_file_path = (
'/tmp/autopilot/tests.test_simple.SimpleTest.test_simple.ogv')
self.addCleanup(remove_if_exists, video_file_path)
code, output, error = self.run_autopilot(["run", "-r", "tests"])
self.assertThat(code, Equals(0))
self.assertThat(os.path.exists(video_file_path), Equals(False))
@skipIf(platform.model() != "Desktop", "Only suitable on Desktop (VidRec)")
def test_no_video_for_nested_testcase_when_parent_and_child_fail(self):
"""Test recording must not create a new recording for nested testcases
where both the parent and the child testcase fail.
"""
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
import os
class OuterTestCase(AutopilotTestCase):
def test_nested_classes(self):
class InnerTestCase(AutopilotTestCase):
def test_will_fail(self):
self.assertTrue(False)
InnerTestCase("test_will_fail").run()
self.assertTrue(False)
""")
)
expected_video_file = (
'/tmp/autopilot/tests.test_simple.OuterTestCase.'
'test_nested_classes.ogv')
erroneous_video_file = (
'/tmp/autopilot/tests.test_simple.OuterTestCase.'
'test_nested_classes.InnerTestCase.test_will_fail.ogv')
self.addCleanup(remove_if_exists, expected_video_file)
self.addCleanup(remove_if_exists, erroneous_video_file)
code, output, error = self.run_autopilot(["run", "-v", "-r", "tests"])
self.assertThat(code, Equals(1))
self.assertThat(os.path.exists(expected_video_file), Equals(True))
self.assertThat(os.path.exists(erroneous_video_file), Equals(False))
def test_runs_with_import_errors_fail(self):
"""Import errors inside a test must be considered a test failure."""
self.create_test_file(
'test_simple.py', dedent("""\
from autopilot.testcase import AutopilotTestCase
# create an import error:
import asdjkhdfjgsdhfjhsd
class SimpleTest(AutopilotTestCase):
def test_simple(self):
pass
""")
)
code, output, error = self.run_autopilot(["run", "tests"])
self.assertThat(code, Equals(1))
self.assertThat(error, Equals(''))
self.assertThat(
output,
MatchesRegex(
".*ImportError: No module named [']?asdjkhdfjgsdhfjhsd[']?.*",
re.DOTALL
)
)
self.assertThat(output, Contains("FAILED (failures=1)"))
def test_runs_with_syntax_errors_fail(self):
"""Import errors inside a test must be considered a test failure."""
self.create_test_file(
'test_simple.py', dedent("""\
from autopilot.testcase import AutopilotTestCase
# create a syntax error:
..
class SimpleTest(AutopilotTestCase):
def test_simple(self):
pass
""")
)
code, output, error = self.run_autopilot(["run", "tests"])
expected_error = '''\
tests/test_simple.py", line 4
..
^
SyntaxError: invalid syntax
'''
self.assertThat(code, Equals(1))
self.assertThat(error, Equals(''))
self.assertThat(output, Contains(expected_error))
self.assertThat(output, Contains("FAILED (failures=1)"))
def test_can_create_subunit_result_file(self):
self.create_test_file(
"test_simple.py", dedent(u"""\
from autopilot.testcase import AutopilotTestCase
class SimpleTest(AutopilotTestCase):
def test_simple(self):
pass
""")
)
output_file_path = mktemp()
self.addCleanup(remove_if_exists, output_file_path)
code, output, error = self.run_autopilot([
"run",
"-o", output_file_path,
"-f", "subunit",
"tests"])
self.assertThat(code, Equals(0))
self.assertTrue(os.path.exists(output_file_path))
def test_launch_needs_arguments(self):
"""Autopilot launch must complain if not given an application to
launch."""
rc, _, _ = self.run_autopilot(["launch"])
self.assertThat(rc, Equals(2))
def test_complains_on_unknown_introspection_type(self):
"""Launching a binary that does not support an introspection type we
are familiar with must result in a nice error message.
"""
rc, stdout, _ = self.run_autopilot(["launch", "yes"])
self.assertThat(rc, Equals(1))
self.assertThat(
stdout,
Contains(
"Error: Could not determine introspection type to use for "
"application '/usr/bin/yes'"))
def test_complains_on_missing_file(self):
"""Must give a nice error message if we try and launch a binary that's
missing."""
rc, stdout, _ = self.run_autopilot(["launch", "DoEsNotExist"])
self.assertThat(rc, Equals(1))
self.assertThat(
stdout, Contains("Error: Cannot find application 'DoEsNotExist'"))
def test_complains_on_non_dynamic_binary(self):
"""Must give a nice error message when passing in a non-dynamic
binary."""
# tzselect is a bash script, and is in the base system, so should
# always exist.
rc, stdout, _ = self.run_autopilot(["launch", "tzselect"])
self.assertThat(rc, Equals(1))
self.assertThat(
stdout, Contains(
"Error detecting launcher: Command '['ldd', "
"'/usr/bin/tzselect']' returned non-zero exit status 1\n"
"(Perhaps use the '-i' argument to specify an interface.)\n")
)
def test_run_random_order_flag_works(self):
"""Must run tests in random order when -ro is used"""
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
from time import sleep
class SimpleTest(AutopilotTestCase):
def test_simple_one(self):
pass
def test_simple_two(self):
pass
""")
)
code, output, error = self.run_autopilot(["run", "-ro", "tests"])
self.assertThat(code, Equals(0))
self.assertThat(output, Contains('Running tests in random order'))
def test_run_random_flag_not_used(self):
"""Must not run tests in random order when -ro is not used"""
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
from time import sleep
class SimpleTest(AutopilotTestCase):
def test_simple_one(self):
pass
def test_simple_two(self):
pass
""")
)
code, output, error = self.run_autopilot(["run", "tests"])
self.assertThat(code, Equals(0))
self.assertThat(output, Not(Contains('Running tests in random order')))
class AutopilotPatchEnvironmentTests(AutopilotTestCase):
def test_patch_environment_new_patch_is_unset_to_none(self):
"""patch_environment must unset the environment variable if previously
was unset.
"""
class PatchEnvironmentSubTests(AutopilotTestCase):
def test_patch_env_sets_var(self):
"""Setting the environment variable must make it available."""
self.patch_environment("APABC321", "Foo")
self.assertThat(os.getenv("APABC321"), Equals("Foo"))
self.assertThat(os.getenv('APABC321'), Equals(None))
result = PatchEnvironmentSubTests("test_patch_env_sets_var").run()
self.assertThat(result.wasSuccessful(), Equals(True))
self.assertThat(os.getenv('APABC321'), Equals(None))
def test_patch_environment_existing_patch_is_reset(self):
"""patch_environment must reset the environment back to it's previous
value.
"""
class PatchEnvironmentSubTests(AutopilotTestCase):
def test_patch_env_sets_var(self):
"""Setting the environment variable must make it available."""
self.patch_environment("APABC987", "InnerTest")
self.assertThat(os.getenv("APABC987"), Equals("InnerTest"))
self.patch_environment('APABC987', "OuterTest")
self.assertThat(os.getenv('APABC987'), Equals("OuterTest"))
result = PatchEnvironmentSubTests("test_patch_env_sets_var").run()
self.assertThat(result.wasSuccessful(), Equals(True))
self.assertThat(os.getenv('APABC987'), Equals("OuterTest"))
class AutopilotVerboseFunctionalTests(AutopilotFunctionalTestsBase):
"""Scenarioed functional tests for autopilot's verbose logging."""
scenarios = [
('text_format', dict(output_format='text')),
('xml_format', dict(output_format='xml'))
]
def test_verbose_flag_works(self):
"""Verbose flag must log to stderr."""
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
class SimpleTest(AutopilotTestCase):
def test_simple(self):
pass
""")
)
code, output, error = self.run_autopilot(["run",
"-f", self.output_format,
"-v", "tests"])
self.assertThat(code, Equals(0))
self.assertThat(
error, Contains(
"Starting test tests.test_simple.SimpleTest.test_simple"))
def test_verbose_flag_shows_timestamps(self):
"""Verbose log must include timestamps."""
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
class SimpleTest(AutopilotTestCase):
def test_simple(self):
pass
""")
)
code, output, error = self.run_autopilot(["run",
"-f", self.output_format,
"-v", "tests"])
self.assertThat(error, MatchesRegex("^\d\d:\d\d:\d\d\.\d\d\d"))
def test_verbose_flag_shows_success(self):
"""Verbose log must indicate successful tests (text format)."""
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
class SimpleTest(AutopilotTestCase):
def test_simple(self):
pass
""")
)
code, output, error = self.run_autopilot(["run",
"-f", self.output_format,
"-v", "tests"])
self.assertThat(
error, Contains("OK: tests.test_simple.SimpleTest.test_simple"))
def test_verbose_flag_shows_error(self):
"""Verbose log must indicate test error with a traceback."""
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
class SimpleTest(AutopilotTestCase):
def test_simple(self):
raise RuntimeError("Intentionally fail test.")
""")
)
code, output, error = self.run_autopilot(["run",
"-f", self.output_format,
"-v", "tests"])
self.assertThat(
error, Contains("ERROR: tests.test_simple.SimpleTest.test_simple"))
self.assertThat(error, Contains("traceback:"))
self.assertThat(
error,
Contains("RuntimeError: Intentionally fail test.")
)
def test_verbose_flag_shows_failure(self):
"""Verbose log must indicate a test failure with a traceback (xml
format)."""
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
class SimpleTest(AutopilotTestCase):
def test_simple(self):
self.assertTrue(False)
""")
)
code, output, error = self.run_autopilot(["run",
"-f", self.output_format,
"-v", "tests"])
self.assertIn("FAIL: tests.test_simple.SimpleTest.test_simple", error)
self.assertIn("traceback:", error)
self.assertIn("AssertionError: False is not true", error)
def test_verbose_flag_captures_nested_autopilottestcase_classes(self):
"""Verbose log must contain the log details of both the nested and
parent testcase."""
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
import os
class OuterTestCase(AutopilotTestCase):
def test_nested_classes(self):
class InnerTestCase(AutopilotTestCase):
def test_produce_log_output(self):
self.assertTrue(True)
InnerTestCase("test_produce_log_output").run()
self.assertTrue(True)
""")
)
code, output, error = self.run_autopilot(["run",
"-f", self.output_format,
"-v", "tests"])
self.assertThat(code, Equals(0))
self.assertThat(
error, Contains(
"Starting test tests.test_simple.OuterTestCase."
"test_nested_classes"))
self.assertThat(
error, Contains(
"Starting test tests.test_simple.InnerTestCase."
"test_produce_log_output"))
def test_can_enable_debug_output(self):
"""Verbose log must show debug messages if we specify '-vv'."""
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
from autopilot.utilities import get_debug_logger
class SimpleTest(AutopilotTestCase):
def test_simple(self):
get_debug_logger().debug("Hello World")
""")
)
code, output, error = self.run_autopilot(["run",
"-f", self.output_format,
"-vv", "tests"])
self.assertThat(error, Contains("Hello World"))
def test_debug_output_not_shown_by_default(self):
"""Verbose log must not show debug messages unless we specify '-vv'."""
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
from autopilot.utilities import get_debug_logger
class SimpleTest(AutopilotTestCase):
def test_simple(self):
get_debug_logger().debug("Hello World")
""")
)
code, output, error = self.run_autopilot(["run",
"-f", self.output_format,
"-v", "tests"])
self.assertThat(error, Not(Contains("Hello World")))
def test_verbose_flag_shows_autopilot_version(self):
from autopilot import get_version_string
"""Verbose log must indicate successful tests (text format)."""
self.create_test_file(
"test_simple.py", dedent("""\
from autopilot.testcase import AutopilotTestCase
class SimpleTest(AutopilotTestCase):
def test_simple(self):
pass
""")
)
code, output, error = self.run_autopilot(["run",
"-f", self.output_format,
"-v", "tests"])
self.assertThat(
error, Contains(get_version_string()))
def test_failfast(self):
"""Run stops after first error encountered."""
self.create_test_file(
'test_failfast.py', dedent("""\
from autopilot.testcase import AutopilotTestCase
class SimpleTest(AutopilotTestCase):
def test_one(self):
raise Exception
def test_two(self):
raise Exception
""")
)
code, output, error = self.run_autopilot(["run",
"--failfast",
"tests"])
self.assertThat(code, Equals(1))
self.assertIn("Ran 1 test", output)
self.assertIn("FAILED (failures=1)", output)
|