Loading video player…

Simulate a Text File in Python (Overview)

Testing applications that read files from a disk can be challenging. Issues such as machine dependencies, special access requirements, and slow performance often arise when you need to read text from a file.

In this Code Conversation with instructor Martin Breuss, you’ll discover how to simplify this process by simulating text files with StringIO from the io module in Python’s standard library.

In this video course, you’ll learn how to:

  • Use io.StringIO to simulate a text file on disk
  • Perform file operations on a io.StringIO object
  • Decide when to use io.StringIO and when to avoid it
  • Understand possible alternatives
  • Mock a file object using unittest.mock

Understanding how to simulate text file objects and mocking file objects can help to streamline your testing strategy and your development process.

Download

Sample Code (.zip)

1.0 KB
Download

Course Slides (.pdf)

4.0 MB

Become a Member to join the conversation.