File

Defines a file entity on disk that can have a number of checks done on it. File objects can be extended via subclassing to allow creating new file objects with custom functionality to make dealing with different file types easier to manage and test against.

class File
GetSize()

The size of the file items in bytes.

Returns:

The size in bytes or None if it does not exist.

WriteAppendOn(content, event=None)

Append content to a file. If the file does not exist, it will create a new file.

Parameters:
  • content – A string containing the content to write A function can be provided that will write the data itself. This function takes accepts one argument of the file object to use to write to the file.

  • event – The event it should write on. By default, this is the Starting event.

Examples

Write new data in a file, causing it to be different on different event

f = tr.Disk.File("foo.c")
d.WriteAppendOn("//changed file",tr.FinishedEvent)
WriteCustomOn(func, event=None)

Takes a function that will write data to a file when a certain event goes off. The functions will be passed a string for the filename to write to. The function is responsible for opening and closing the file. It should return values based on what testers.Lambda accepts to report the issue to the user.

Parameters:
  • func – A function that takes a file name. This function that will open and write data.

  • event – The event it should write on. By default, this is the Starting event.

WriteOn(content, event=None)

Writes out content to a replacing any existing content in the file.

Parameters:
  • content – A string containing the content to write A function can be provided that will write the data itself. This function takes accepts one argument of the file object to use to write to the file.

  • event – The event that the write should be trigger with. By default, this is the Starting event.

Examples

Write new data in a file

f = tr.Disk.File("data.json")
d.WriteOn("{data=1}")
property AbsPath

Absolute path of the file based on the default runtime behavior. This is normally the runtime path which maps to the sandbox directory.

property AbsRunTimePath

Absolute path of the file based on the runtime path which maps to the sandbox directory.

property AbsTestPath

Absolute path of the file based on the test path or the location in which the original test file exists.

property Name: str

The name of the file. This is general the path to the file relative to the testing or runtime root. It can be an absolute path as well given the file was defined that way.

Getter:

Returns name of file

Return type:

str

Testable properties:

These are the testable properties of the file object.

property Exists

Test that the Directory exists or does not exist. If set to None no test will happen

Event : Finished

Default type : Boolean

Example:

Test that a file exists at the end of the Test run

tr = Test.AddTestRun()
f = tr.Disk.File("logs/errors.log")
f.Exists = True
property Size

Test that the size is equal the value provided

Event : Finished

Default type : integer

Example

Test that a file size is 1024 bytes.

tr = Test.AddTestRun()
f = tr.Disk.File("some.data")
f.Size = 1024

Test that a file size is less than 1096 bytes.

tr = Test.AddTestRun()
f = tr.Disk.File("some.data")
f.Size = Testers.LessThan(1096)
property Content

Test that the content matches the provided gold file

Event : Finished

Default type : string ( i.e. file name to gold file)

Example:

Test that output file matches expected results

tr = Test.AddTestRun()
f = tr.Disk.File("out.data")
f.Content = "gold/content.gold"