0
0
PyTesttesting~10 mins

monkeypatch.chdir in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to change the current working directory using monkeypatch.

PyTest
def test_change_dir(monkeypatch):
    monkeypatch.[1]('/tmp')
    import os
    assert os.getcwd() == '/tmp'
Drag options to blanks, or click blank then click option'
Achdir
Bsetenv
Csetattr
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using monkeypatch.setenv instead of monkeypatch.chdir
Trying to use monkeypatch.setattr for changing directory
2fill in blank
medium

Complete the code to verify the directory change inside the test.

PyTest
def test_dir(monkeypatch):
    monkeypatch.chdir('/var')
    import os
    assert os.getcwd() == [1]
Drag options to blanks, or click blank then click option'
A'/var'
B'/tmp'
C'/home'
D'/'
Attempts:
3 left
💡 Hint
Common Mistakes
Asserting the wrong directory path
Confusing '/tmp' and '/var'
3fill in blank
hard

Fix the error in the test code to correctly change directory using monkeypatch.

PyTest
def test_fix_dir(monkeypatch):
    monkeypatch.[1]('/home/user')
    import os
    assert os.getcwd() == '/home/user'
Drag options to blanks, or click blank then click option'
Asetenv
Bsetattr
Cchdir
Dremove
Attempts:
3 left
💡 Hint
Common Mistakes
Using monkeypatch.setenv or monkeypatch.setattr instead of monkeypatch.chdir
4fill in blank
hard

Fill both blanks to create a test that changes directory and asserts the new path.

PyTest
def test_dir_change(monkeypatch):
    monkeypatch.[1]('/etc')
    import os
    assert os.getcwd() == [2]
Drag options to blanks, or click blank then click option'
Achdir
B'/var'
C'/etc'
Dsetenv
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up method names and directory strings
Using wrong directory path in assertion
5fill in blank
hard

Fill all three blanks to write a test that changes directory, imports os, and asserts the current directory.

PyTest
def test_full(monkeypatch):
    monkeypatch.[1]([2])
    import os
    assert os.getcwd() == [3]
Drag options to blanks, or click blank then click option'
Achdir
B'/opt'
Dsetenv
Attempts:
3 left
💡 Hint
Common Mistakes
Using setenv instead of chdir
Mismatching directory strings