Question

In: Computer Science

how to write a unit test for a write function in python? is there a better...

how to write a unit test for a write function in python? is there a better way than running main once and checking whether the new file path exists?

Solutions

Expert Solution

How to write a unit test for a write function in python?

A method by which individual units of ASCII document. In programing, unit testing could be a method by which individual units of ASCII document, sets of one or more bug modules along with associated control data, usage procedures, and operating procedures, are tested to see if they're suitable use.

Let's take a example of factorial code --->

import sys def fact(n): if n == 0: return 1 return n * fact(n -1) def div(n): res = 10 / n return res def main(n): res = fact(n) print(res) if __name__ == '__main__': if len(sys.argv) > 1: main(int(sys.argv[1]))

Now we will write our first test case.--->

from factorial import fact class TestFactorial(unittest.TestCase): def test_fact(self): # Any method which starts with ``test_`` will considered as a test case. res = fact(5) self.assertEqual(res, 120) if __name__ == '__main__': unittest.main()

is there a better way than running main once and checking whether the new file path exists?

Yes --->

The os.path module provides some functions for working with path names. The module is accessible for both Python 2 & Pythn 3.

The foremost important functions are:-
os.path.exists(path) - Returns true if the trail could be a file, directory, or a sound symlink.
os.path.isfile(path) - Returns true if the trail may be a regular file or a symlink to a file.
os.path.isdir(path) - Returns true if the trail could be a directory or a symlink to a directory.

Example -->

import os.path

if os.path.isfile('filename.txt'):
print ("File exist")
else:
print ("File not exist")


Related Solutions

Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function...
Using Python C++ Purpose: Write and test a non-trivial 2-d array function. Write a user-defined function that, given an arbitrary 2-d array as input, returns its perimeter sum, which is defined as the sum of all the elements along its perimeter. You must name your function perimeter_sum
PleaSe write a GENERATOR function in Python: how to write it with yield ? XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Thanks...
PleaSe write a GENERATOR function in Python: how to write it with yield ? XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Thanks in advance Write /the/ generator /function/ genAccum(seq, fn), which takes in an iterable and a function fn and yields each accumulated value from applying fn to the running total and the next element. (Note: to convert an iterable into an iterator you use iter(iterable)) ''' >>> add = lambda x, y: x + y >>> mul = lambda x, y: x * y >>>...
Python: How would I write a function that takes a directory and a size in bytes,...
Python: How would I write a function that takes a directory and a size in bytes, and returns a list of files in the directory or below that are larger than the size. For example, I can use this function to look for files larger than 1 Meg below my Home directory.
Python: How would I write a function that takes a URL of a webpage and finds...
Python: How would I write a function that takes a URL of a webpage and finds the first link on the page? The hint given is the function should return a tuple holding two strings: the URL and the link text.
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
In python write a function whose input is a string. This function determines the data type...
In python write a function whose input is a string. This function determines the data type of the input string. The data types can be a float, int, or string. Most pass the following assertions: assert determine_data_type('1.2') == float assert determine_data_type('4') == int assert determine_data_type('EAS503') == str
Write a python function to fulfill the requirements. The function accepts a string, a current state,...
Write a python function to fulfill the requirements. The function accepts a string, a current state, edges’ information, and an accepting state. The output of the function is a boolean value verifying if the string can pass the finite state machine or not.             ### Finite State Machine Simulator in Python ### Provide s1 and s2 that are both accepted, but s1 != s2. s1 = "bdf" s2 = "bdgbdf" edges = {(1,'a') : 2,                (1,'b') : 3,       ...
This is python: #Write a function called count_positive_evens. This function #should take as input a list...
This is python: #Write a function called count_positive_evens. This function #should take as input a list of integers, and return as #output a single integer. The number the function returns #should be the count of numbers from the list that were both #positive and even. # #For example: # # count_positive_evens([5, 7, 9, 8, -1, -2, -3]) -> 1 # count_positive_evens([2, 4, 6, 8, 10, 12, 15]) -> 6 # count_positive_evens([-2, -4, -6, -8, -10, 1]) -> 0 # #0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT