|
| 1 | +class TestQueryCount(object): |
| 2 | + """Test report generated by --querycount parameter""" |
| 3 | + |
| 4 | + def test_querycount_report_header(self, django_testdir): |
| 5 | + django_testdir.create_test_module(''' |
| 6 | + def test_zero_queries(): |
| 7 | + pass |
| 8 | + ''') |
| 9 | + |
| 10 | + result = django_testdir.runpytest_subprocess('--querycount=5') |
| 11 | + result.stdout.fnmatch_lines([ |
| 12 | + '*== top 5 tests with most queries ==*' |
| 13 | + ]) |
| 14 | + |
| 15 | + def test_header_not_set_without_parameter(self, django_testdir): |
| 16 | + django_testdir.create_test_module(''' |
| 17 | + def test_zero_queries(): |
| 18 | + pass |
| 19 | + ''') |
| 20 | + |
| 21 | + result = django_testdir.runpytest_subprocess() |
| 22 | + assert 'tests with most queries' not in result.stdout.str() |
| 23 | + |
| 24 | + def test_querycount_report_lines(self, django_testdir): |
| 25 | + django_testdir.create_test_module(''' |
| 26 | + import pytest |
| 27 | + from django.db import connection |
| 28 | +
|
| 29 | + @pytest.mark.django_db |
| 30 | + def test_one_query(): |
| 31 | + with connection.cursor() as cursor: |
| 32 | + cursor.execute('SELECT 1') |
| 33 | +
|
| 34 | + assert True |
| 35 | +
|
| 36 | + @pytest.mark.django_db |
| 37 | + def test_two_queries(): |
| 38 | + with connection.cursor() as cursor: |
| 39 | + cursor.execute('SELECT 1') |
| 40 | + cursor.execute('SELECT 1') |
| 41 | +
|
| 42 | + assert True |
| 43 | +
|
| 44 | + @pytest.mark.django_db |
| 45 | + def test_failed_one_query(): |
| 46 | + with connection.cursor() as cursor: |
| 47 | + cursor.execute('SELECT 1') |
| 48 | +
|
| 49 | + assert False |
| 50 | +
|
| 51 | + def test_zero_queries(): |
| 52 | + assert True |
| 53 | + ''') |
| 54 | + |
| 55 | + result = django_testdir.runpytest_subprocess('--querycount=3') |
| 56 | + lines = result.stdout.get_lines_after( |
| 57 | + '*top 3 tests with most queries*' |
| 58 | + ) |
| 59 | + assert 'test_two_queries' in lines[0] |
| 60 | + assert 'test_one_query' in lines[1] |
| 61 | + assert 'test_failed' in lines[2] |
| 62 | + assert 'test_zero_queries' not in result.stdout.str() |
| 63 | + |
| 64 | + def test_report_all_lines_on_querycount_zero(self, django_testdir): |
| 65 | + django_testdir.create_test_module(''' |
| 66 | + import pytest |
| 67 | + from django.db import connection |
| 68 | +
|
| 69 | + @pytest.mark.django_db |
| 70 | + def test_one_query(): |
| 71 | + with connection.cursor() as cursor: |
| 72 | + cursor.execute('SELECT 1') |
| 73 | +
|
| 74 | + assert True |
| 75 | +
|
| 76 | + @pytest.mark.django_db |
| 77 | + def test_two_queries(): |
| 78 | + with connection.cursor() as cursor: |
| 79 | + cursor.execute('SELECT 1') |
| 80 | + cursor.execute('SELECT 1') |
| 81 | +
|
| 82 | + assert True |
| 83 | + ''') |
| 84 | + |
| 85 | + result = django_testdir.runpytest_subprocess('--querycount=0') |
| 86 | + lines = result.stdout.get_lines_after( |
| 87 | + '*top tests with most queries*' |
| 88 | + ) |
| 89 | + assert 'test_two_queries' in lines[0] |
| 90 | + assert 'test_one_query' in lines[1] |
0 commit comments