Coverage for tests/plugin_test.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2024-04-03 22:19 +0200

1from json import loads 

2 

3from pytest import ExitCode, Pytester 

4 

5 

6def test_throws_if_called_without_file_path(pytester: Pytester) -> None: 

7 result = pytester.runpytest("--gitlab-code-quality-report") 

8 assert "argument --gitlab-code-quality-report: expected one argument" in str( 

9 result.stderr 

10 ) 

11 

12 

13def test_works_for_empty_tests(pytester: Pytester) -> None: 

14 result = pytester.runpytest("--gitlab-code-quality-report", "pytest-warnings.json") 

15 assert result.ret == ExitCode.NO_TESTS_COLLECTED 

16 

17 report_file = pytester.path / "pytest-warnings.json" 

18 assert report_file.exists() 

19 assert loads(report_file.read_text()) == [] 

20 

21 

22def test_works_for_manually_emitted_warning(pytester: Pytester) -> None: 

23 pytester.makepyfile(""" 

24 import warnings 

25 

26 def test_no_warning(): 

27 assert 1 == 1 

28 

29 def test_has_warning(): 

30 warnings.warn("beware!") 

31 """) 

32 result = pytester.runpytest("--gitlab-code-quality-report", "pytest-warnings.json") 

33 assert result.ret == 0 

34 

35 report_file = pytester.path / "pytest-warnings.json" 

36 assert report_file.exists() 

37 

38 warnings = loads(report_file.read_text()) 

39 assert len(warnings) == 1 

40 assert warnings[0]["description"] == "beware!" 

41 assert warnings[0]["check_name"] == "PytestUserWarning" 

42 assert ( 

43 warnings[0]["location"]["path"] == "test_works_for_manually_emitted_warning.py" 

44 ) 

45 assert warnings[0]["location"]["lines"]["begin"] == 7