tests.rake 5.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
# Set up the clean and clobber tasks
CLOBBER.include(REPORT_DIR, 'test_root/*_repo', 'test_root/staticfiles')

def run_under_coverage(cmd, root)
    cmd0, cmd_rest = cmd.split(" ", 2)
    # We use "python -m coverage" so that the proper python will run the importable coverage
    # rather than the coverage that OS path finds.
    cmd = "python -m coverage run --rcfile=#{root}/.coveragerc `which #{cmd0}` #{cmd_rest}"
    return cmd
end

12
def run_tests(system, report_dir, test_id=nil, stop_on_failure=true)
13 14
    ENV['NOSE_XUNIT_FILE'] = File.join(report_dir, "nosetests.xml")
    dirs = Dir["common/djangoapps/*"] + Dir["#{system}/djangoapps/*"]
15 16
    test_id = dirs.join(' ') if test_id.nil? or test_id == ''
    cmd = django_admin(system, :test, 'test', '--logging-clear-handlers', test_id)
17
    test_sh(run_under_coverage(cmd, system))
18 19
end

20
def run_acceptance_tests(system, report_dir, harvest_args)
21 22 23 24 25 26 27 28 29 30
    # HACK: Since now the CMS depends on the existence of some database tables
    # that used to be in LMS (Role/Permissions for Forums) we need to make
    # sure the acceptance tests create/migrate the database tables
    # that are represented in the LMS. We might be able to address this by moving
    # out the migrations from lms/django_comment_client, but then we'd have to
    # repair all the existing migrations from the upgrade tables in the DB.
    if system == :cms
        sh(django_admin('lms', 'acceptance', 'syncdb', '--noinput'))
        sh(django_admin('lms', 'acceptance', 'migrate', '--noinput'))
    end
31 32
    sh(django_admin(system, 'acceptance', 'syncdb', '--noinput'))
    sh(django_admin(system, 'acceptance', 'migrate', '--noinput'))
33
    test_sh(django_admin(system, 'acceptance', 'harvest', '--debug-mode', '--tag -skip', harvest_args))
34 35 36
end


37 38 39 40 41 42 43 44 45 46 47 48 49
directory REPORT_DIR

task :clean_test_files do
    sh("git clean -fqdx test_root")
end

TEST_TASK_DIRS = []

[:lms, :cms].each do |system|
    report_dir = report_dir_path(system)

    # Per System tasks
    desc "Run all django tests on our djangoapps for the #{system}"
50
    task "test_#{system}", [:test_id] => ["clean_test_files", :predjango, "#{system}:gather_assets:test", "fasttest_#{system}"]
51 52 53

    # Have a way to run the tests without running collectstatic -- useful when debugging without
    # messing with static files.
54 55 56
    task "fasttest_#{system}", [:test_id] => [report_dir, :install_prereqs, :predjango] do |t, args|
        args.with_defaults(:test_id => nil)
        run_tests(system, report_dir, args.test_id)
57 58
    end

59 60 61 62 63 64 65 66 67 68 69
    # Run acceptance tests
    desc "Run acceptance tests"
    task "test_acceptance_#{system}", [:harvest_args] => ["#{system}:gather_assets:acceptance", "fasttest_acceptance_#{system}"]

    desc "Run acceptance tests without collectstatic"
    task "fasttest_acceptance_#{system}", [:harvest_args] => ["clean_test_files", :predjango, report_dir] do |t, args|
        args.with_defaults(:harvest_args => '')
        run_acceptance_tests(system, report_dir, args.harvest_args)
    end


70 71 72 73 74 75 76 77 78 79
    task :fasttest => "fasttest_#{system}"

    TEST_TASK_DIRS << system
end

Dir["common/lib/*"].select{|lib| File.directory?(lib)}.each do |lib|

    report_dir = report_dir_path(lib)

    desc "Run tests for common lib #{lib}"
80
    task "test_#{lib}"  => ["clean_test_files", report_dir] do
81 82
        ENV['NOSE_XUNIT_FILE'] = File.join(report_dir, "nosetests.xml")
        cmd = "nosetests #{lib}"
83
        test_sh(run_under_coverage(cmd, lib))
84 85 86
    end
    TEST_TASK_DIRS << lib

87 88 89 90 91 92 93
    # There used to be a fasttest_#{lib} command that ran without coverage.
    # However, this is an inconsistent usage of "fast":
    # When running tests for lms and cms, "fast" means skipping
    # staticfiles collection, but still running under coverage.
    # We keep the fasttest_#{lib} command for backwards compatibility,
    # but make it an alias to the normal test command.
    task "fasttest_#{lib}" => "test_#{lib}"
94 95 96 97 98 99 100 101
end

task :report_dirs

TEST_TASK_DIRS.each do |dir|
    report_dir = report_dir_path(dir)
    directory report_dir
    task :report_dirs => [REPORT_DIR, report_dir]
102
    task :test => "test_#{dir}"
103 104
end

105 106
desc "Run all tests"
task :test
107

108 109
desc "Build the html, xml, and diff coverage reports"
task :coverage => :report_dirs do
110 111 112

    found_coverage_info = false

113 114
    TEST_TASK_DIRS.each do |dir|
        report_dir = report_dir_path(dir)
115

116 117
        if !File.file?("#{report_dir}/.coverage")
            next
118 119
        else
            found_coverage_info = true
120 121
        end

122
        # Generate the coverage.py HTML report
123
        sh("coverage html --rcfile=#{dir}/.coveragerc")
124 125

        # Generate the coverage.py XML report
126
        sh("coverage xml -o #{report_dir}/coverage.xml --rcfile=#{dir}/.coveragerc")
127 128

        # Generate the diff coverage HTML report, based on the XML report
129
        sh("diff-cover #{report_dir}/coverage.xml --html-report #{report_dir}/diff_cover.html")
130 131

        # Print the diff coverage report to the console
132
        sh("diff-cover #{report_dir}/coverage.xml")
133 134 135 136 137
        puts "\n"
    end

    if not found_coverage_info
        puts "No coverage info found.  Run `rake test` before running `rake coverage`."
138
    end
139
end