Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

writing a pytest function to check outputting to a file in python?

Tags:

I asked this question about how to write a pytest to check output in stdout and got a solution. Now I need to write a test case, to check if the contents are written to the file and that the contents are written as expected eg:

def writetoafile():    file = open("output.txt",w)    file.write("hello\n")    file.write("world\n")    file.close() 

now a pytest function to check if it written:

def test_writeToFile():     file = open("ouput.txt",'r')     expected = "hello\nworld\n"     assert expected==file.read() 

while this seems to work, I do not think this is ideal, especially hard-coding. how are these kind of test functions of writing to a file usually written?

like image 725
brain storm Avatar asked Dec 11 '13 22:12

brain storm


People also ask

How do I pytest a specific file?

Running pytest We can run a specific test file by giving its name as an argument. A specific function can be run by providing its name after the :: characters. Markers can be used to group tests. A marked grouped of tests is then run with pytest -m .

How do I get a pytest report?

To generate a HTML report for a Selenium test, we have to install a plugin with the command: pip install pytest-html. To generate the report, we have to move from the current directory to the directory of the Pytest file that we want to execute. Then run the command: pytest --html=report.

What is pytest check?

A pytest plugin that allows multiple failures per test. This pytest plugin was a rewrite and a rename of pytest-expect.


2 Answers

There is the tmpdir fixture which will create you a per-test temporary directory. So a test would look something like this:

def writetoafile(fname):     with open(fname, 'w') as fp:         fp.write('Hello\n')  def test_writetofile(tmpdir):     file = tmpdir.join('output.txt')     writetoafile(file.strpath)  # or use str(file)     assert file.read() == 'Hello\n' 

Here you're refactoring the code to not be hardcoded either, which is a prime example of how testing your code makes you design it better.

like image 52
flub Avatar answered Sep 19 '22 17:09

flub


Suppose you have this "amazing" piece of software in a file called main.py:

""" main.py """  def write_to_file(text):     with open("output.txt", "w") as h:         h.write(text)  if __name__ == "__main__":     write_to_file("Every great dream begins with a dreamer.") 

To test the write_to_file method, you can write something like this in a file in the same folder called test_main.py:

""" test_main.py """ from unittest.mock import patch, mock_open  import main   def test_do_stuff_with_file():     open_mock = mock_open()     with patch("main.open", open_mock, create=True):         main.write_to_file("test-data")      open_mock.assert_called_with("output.txt", "w")     open_mock.return_value.write.assert_called_once_with("test-data") 

I always try to avoid writing files to disk, even if it's a temporary folder dedicated to my tests: not actually touching the disk makes your tests much faster, especially if you interact with files a lot in your code.

like image 26
Enrico M. Avatar answered Sep 18 '22 17:09

Enrico M.