# pytest

***

**1. Basic Assertions**

```python
def test_simple_assertion():
    assert 1 == 1
```

**2. Failing Assertions**

```python
def test_failing_assertion():
    assert 1 == 2
```

**3. Custom Error Messages**

```python
def test_custom_error_message():
    assert 1 == 2, "Custom error message"
```

**4. Skipping Tests**

```python
@pytest.mark.skip
def test_skipped():
    pass
```

**5. Parameterizing Tests**

```python
@pytest.mark.parametrize("x", [1, 2, 3])
def test_parameterized(x):
    assert x > 0
```

**6. Fixture Functions**

```python
@pytest.fixture
def my_fixture():
    return 1

def test_using_fixture(my_fixture):
    assert my_fixture == 1
```

**7. Mocks**

```python
@pytest.fixture
def mock_foo(mocker):
    return mocker.MagicMock()

def test_using_mock(mock_foo):
    mock_foo.bar()
    mock_foo.bar.assert_called_once()
```

**8. Monkeypatching**

```python
@pytest.fixture
def monkeypatch(request):
    mock_function = MagicMock()
    monkeypatch.setattr(module, "function", mock_function)

def test_monkeypatching(monkeypatch):
    pass
```

**9. Capturing Output**

```python
def test_capture_output(capsys):
    print("hello")
    captured = capsys.readouterr()
    assert "hello" in captured.out
```

**10. Running Tests in Parallel**

```python
def test_parallel():
    pass

pytest -n 4
```

**11. Custom Markers**

```python
@pytest.mark.slow
def test_slow():
    pass
```

**12. Skipping Tests Based on Markers**

```python
pytest -m "not slow"
```

**13. XFAIL Tests**

```python
@pytest.mark.xfail
def test_xfail():
    assert False
```

**14. Asserting Exception**

```python
def test_raises():
    with pytest.raises(ValueError):
        raise ValueError
```

**15. Asserting That No Exception Is Raised**

```python
def test_does_not_raise():
    with pytest.raises(Exception):
        pass
```

**16. Reporting Test Failures**

```python
@pytest.mark.parametrize("x", [1, 2, 3])
def test_with_multiple_failures(x):
    assert x > 2
```

**17. Asserting Values**

```python
def test_assert_values():
    assert [1, 2, 3] == [1, 2, 3]
    assert {'a': 1, 'b': 2} == {'b': 2, 'a': 1}
```

**18. Asserting Multiline Strings**

```python
def test_multiline_strings():
    assert """
    This is a multiline string
    that spans multiple lines
    """ == """
    This is a multiline string
    that spans multiple lines
    """
```

**19. Asserting Regular Expressions**

```python
def test_regex():
    assert re.match("foo", "foobar")
```

**20. Asserting Floats**

```python
def test_floats():
    assert 1.234 == pytest.approx(1.23)
```

**21. Asserting Dictionaries**

```python
def test_dicts():
    assert {'a': 1, 'b': 2} == {'b': 2, 'a': 1}
```

**22. Asserting Sets**

```python
def test_sets():
    assert set([1, 2, 3]) == set([3, 1, 2])
```

**23. Asserting Tuples**

```python
def test_tuples():
    assert (1, 2, 3) == (3, 1, 2)
```

**24. Asserting Booleans**

```python
def test_booleans():
    assert True == True
    assert False != True
```

**25. Asserting Nones**

```python
def test_nones():
    assert None == None
```

**26. Using Custom Assertions**

```python
def is_even(x):
    return x % 2 == 0

pytest.register_assert_rewrite("check_even", is_even)

def test_custom_assertion():
    assert_that(4).check_even()
```

**27. Using Plugins**

```python
pytest.main(["-p", "pytest-cov"])
```

**28. Running Tests from a Different Directory**

```python
pytest -v ./other-directory
```

**29. Running Tests in a Virtual Environment**

```python
virtualenv venv
source venv/bin/activate
pip install pytest
pytest
```

**30. Generating a Coverage Report**

```python
pytest --cov=my_module
```

**31. Using the Debugger**

```python
pytest --pdb
```

**32. Setting the Verbosity Level**

```python
pytest -v
```

**33. Running Tests with a Specific Tag**

```python
pytest -m mytag
```

**34. Using the Interactive Prompt**

```python
pytest -i
```

**35. Running Tests by Name**

```python
pytest -k my_test
```

**36. Skipping Tests by Name**

```python
pytest -k -my_test
```

**37. Running Tests on a Specific Platform**

```python
pytest -p darwin
```

**38. Running Tests in a Random Order**

```python
pytest --random
```

**39. Running Tests in a Specific Order**

```python
pytest --junitxml=results.xml
```

**40. Saving Test Results to a File**

```python
pytest --junitxml=results.xml
```

**41. Displaying Test Statistics**

```python
pytest --collect-only
```

**42. Displaying Collected Tests**

```python
pytest --collect-only
```

**43. Running Tests in Isolation**

```python
pytest --isolated
```

**44. Sharing Test State**

```python
pytest --shared
```

**45. Printing Detailed Tracebacks**

```python
pytest -l
```

**46. Disabling Caching of Test Results**

```python
pytest --no_cache
```

**47. Running Tests Faster**

```python
pytest --fast
```

**48. Using the --maxfail Option**

```python
pytest --maxfail=2
```

**49. Using the --maxruns Option**

```python
pytest --maxruns=2
```

**50. Using the --doctest Option**

```python
pytest --doctest
```

**51. Using the --report Option**

```python
pytest --report=json
```

**52. Using the --pdb Option**

```python
pytest --pdb
```

**53. Using the --pdb-lines Option**

```python
pytest --pdb-lines=20
```

**54. Using the --pdb-exact Option**

```python
pytest --pdb-exact
```

**55. Using the --pdb-skip Option**

```python
pytest --pdb-skip
```

**56. Using the --help Option**

```python
pytest --help
```

**57. Using the --version Option**

```python
pytest --version
```

**58. Using the --cov Option**

```python
pytest --cov=my_module
```

**59. Using the --cov-report Option**

```python
pytest --cov-report=html
```

**60. Using the --cov-fail-under Option**

```python
pytest --cov-fail-under=80
```

**61. Using the --junitxml Option**

```python
pytest --junitxml=results.xml
```

**62. Using the --html Option**

```python
pytest --html=report.html
```

**63. Using the --quiet Option**

```python
pytest -q
```

**64. Using the --verbose Option**

```python
pytest -v
```

**65. Using the --debug Option**

```python
pytest -d
```

**66. Using the --log-level Option**

```python
pytest --log-level=DEBUG
```

**67. Using the --log-format Option**

```python
pytest --log-format="%Y-%m-%d %H:%M:%S [%(levelname)s] %(message)s"
```

**68. Using the --showlocals Option**

```python
pytest -s
```

**69. Using the --durations Option**

```python
pytest --durations=4
```

**70. Using the --timings Option**

```python
pytest --timings
```

**71. Using the --import-mode Option**

```python
pytest --import-mode=append
```

**72. Using the --collect-only Option**

```python
pytest --collect-only
```

**73. Using the --maxfail Option**

```python
pytest --maxfail=2
```

**74. Using the --maxruns Option**

```python
pytest --maxruns=2
```

**75. Using the --doctest Option**

```python
pytest --doctest
```

**76. Using the --report Option**

```python
pytest --report=json
```

**77. Using the --pdb Option**

```python
pytest --pdb

```
