1# Running Tests interactively {#sec-running-nixos-tests-interactively} 2 3The test itself can be run interactively. This is particularly useful 4when developing or debugging a test: 5 6```ShellSession 7$ nix-build . -A nixosTests.login.driverInteractive 8$ ./result/bin/nixos-test-driver 9[...] 10>>> 11``` 12 13You can then take any Python statement, e.g. 14 15```py 16>>> start_all() 17>>> test_script() 18>>> machine.succeed("touch /tmp/foo") 19>>> print(machine.succeed("pwd")) # Show stdout of command 20``` 21 22The function `test_script` executes the entire test script and drops you 23back into the test driver command line upon its completion. This allows 24you to inspect the state of the VMs after the test (e.g. to debug the 25test script). 26 27## Shell access in interactive mode {#sec-nixos-test-shell-access} 28 29The function `<yourmachine>.shell_interact()` grants access to a shell running 30inside a virtual machine. To use it, replace `<yourmachine>` with the name of a 31virtual machine defined in the test, for example: `machine.shell_interact()`. 32Keep in mind that this shell may not display everything correctly as it is 33running within an interactive Python REPL, and logging output from the virtual 34machine may overwrite input and output from the guest shell: 35 36```py 37>>> machine.shell_interact() 38machine: Terminal is ready (there is no initial prompt): 39$ hostname 40machine 41``` 42 43As an alternative, you can proxy the guest shell to a local TCP server by first 44starting a TCP server in a terminal using the command: 45 46```ShellSession 47$ socat 'READLINE,PROMPT=$ ' tcp-listen:4444,reuseaddr` 48``` 49 50In the terminal where the test driver is running, connect to this server by 51using: 52 53```py 54>>> machine.shell_interact("tcp:127.0.0.1:4444") 55``` 56 57Once the connection is established, you can enter commands in the socat terminal 58where socat is running. 59 60## Reuse VM state {#sec-nixos-test-reuse-vm-state} 61 62You can re-use the VM states coming from a previous run by setting the 63`--keep-vm-state` flag. 64 65```ShellSession 66$ ./result/bin/nixos-test-driver --keep-vm-state 67``` 68 69The machine state is stored in the `$TMPDIR/vm-state-machinename` 70directory. 71 72## Interactive-only test configuration {#sec-nixos-test-interactive-configuration} 73 74The `.driverInteractive` attribute combines the regular test configuration with 75definitions from the [`interactive` submodule](#test-opt-interactive). This gives you 76a more usable, graphical, but slightly different configuration. 77 78You can add your own interactive-only test configuration by adding extra 79configuration to the [`interactive` submodule](#test-opt-interactive). 80 81To interactively run only the regular configuration, build the `<test>.driver` attribute 82instead, and call it with the flag `result/bin/nixos-test-driver --interactive`.