Pytest - Testing Framework
In this challenge you are supposed to write test cases based on given scenario
Instructions
- Read the instructions and code for the questions given
- file.pickle contains a list of words.
- Use assert function to check or assert
- Write the fixture function unpick which reads the pickle file "file.pickle" and return the list of words
- Mark the function as fixture
- Open the file "file.pickle" with "rb" and load the data using pickle.load function
- Write the test method test1 to check whether the count of words is equal to 6720
- Write the pytest test method test_len_endswith to check whether the count of words ending with 'ing' is greater than 350
- Write the pytest test method test_len_lessthan to check whether the count of words having characters less than 3 is equalto 80
- Write the test method test_keyword to check whether the count of keywords present in the list are not equal to zero
- Write the test method test_word to check whether the word "jupyter" is present in the list
- Write the test method test_len_startswith to check whether the count of words that starts with character 'c' is equal to 700
- Write the test method test_words_ag to check the count of words that starts with 'a' and endwith 'g' is a two digit number and the second digit is zero
- Write the test method char_words to check the whether count of words having alphabets only is greater than 6400
- Write the parameterized test method test_words_starts_with to run against a set of multiple inputs
- Mark the function as parameterize
- Write the function to check the count of words starting with the input character is equal to the output
- [('a',570),('e',327),('h',1296),('t',321)] is the set of inputs and outputs
IDE Instructions
Step 1:Coding
- Once the IDE is opened, Click
File->New->Terminaland run the below commandpip3 install -r requirements.txt - Open test.py file and code for the questions given.
Step 2: Testing
- After Completing the solution, Save the
test.pyfile . - After saving the
test.pyfile , ClickFile->New->Terminaland run the below command to generatetest1.xmlfilepytest test.py --junitxml=test1.xml - Now Click File->New->Terminal and run the below command to run test cases with keyword "len" and generate the test2.xml file
pytest test.py -k len --junitxml=test2.xml - Now test your solution by Click File->New->Terminal and run the below command to run sample test cases
pytest sample_test.py
Step 3 : Submit the Solution
- Once all the sample test cases are passed, click 'Submit' to submit your solution to HackerRank.
Solution
import pytest
import pickle
import keyword
'''
Write the fixture function unpick which reads the pickle file "file.pickle" and return the list of words
Mark the function as fixture
Open the file "file.pickle" with "rb" and load the data using pickle.load function
'''
@pytest.fixture
def unpick():
with open("file.pickle", "rb") as f:
data = pickle.load(f)
return data
'''
Write the test method test1 to check whether the count of words is equal to 6720
'''
def test1(unpick):
words = unpick # This will call the fixture and get the list of words
assert len(words) == 6720, f"Expected 6720 words, but got {len(words)}"
'''
Write the test method test_len_endswith to check whether the count of words ending with 'ing' is greater than 350
'''
def test_len_endswith(unpick):
words = unpick # This will call the fixture and get the list of words
count_ending_with_ing = sum(1 for word in words if word.endswith('ing'))
assert count_ending_with_ing > 350, f"Expected more than 350 words ending with 'ing', but got {count_ending_with_ing}"
'''
Write the test method test_len_lessthan to check whether the count of words having characters less than 3 is equalto 80
'''
def test_len_lessthan(unpick):
words = unpick # This will call the fixture and get the list of words
count_less_than_3 = sum(1 for word in words if len(word) < 3)
assert count_less_than_3 == 80, f"Expected 80 words with less than 3 characters, but got {count_less_than_3}"
'''
Write the test method test_keyword to check whether the count of keywords present in the list are not equal to zero
Hint Use iskeyword function of library keyword.
'''
def test_keyword(unpick):
words = unpick # This will call the fixture and get the list of words
keywords_in_list = [word for word in words if keyword.iskeyword(word)]
assert len(keywords_in_list) > 0, "Expected at least one keyword in the list, but found none."
'''
Write the test method test_word to check whether the word "jupyter" is present in the list
'''
def test_word(unpick):
words = unpick # This will call the fixture and get the list of words
assert "jupyter" in words, "Expected the word 'jupyter' to be present in the list, but it was not found."
'''
Write the test method test_len_startswith to check whether the count of words that starts with character 'c' is equal to 700
'''
def test_len_startswith(unpick):
words = unpick # This will call the fixture and get the list of words
count_starts_with_c = sum(1 for word in words if word.startswith('c'))
assert count_starts_with_c == 700, f"Expected 700 words starting with 'c', but got {count_starts_with_c}"
'''
Write the test method test_words_ag to check the count of words that starts with 'a' and endwith 'g' is a two digit number and the second digit is zero
'''
def test_words_ag(unpick):
words = unpick # This will call the fixture and get the list of words
count_starts_with_a_ends_with_g = sum(1 for word in words if word.startswith('a') and word.endswith('g'))
# Check if the count is a two-digit number and the second digit is zero
assert 10 <= count_starts_with_a_ends_with_g < 100 and count_starts_with_a_ends_with_g % 10 == 0, \
f"Expected a two-digit number ending with zero, but got {count_starts_with_a_ends_with_g}"
'''
Write the test method char_words to check the whether count of words having alphabets only is greater than 6400
'''
def test_char_words(unpick):
words = unpick # This will call the fixture and get the list of words
count_alpha_only = sum(1 for word in words if word.isalpha())
assert count_alpha_only > 6400, f"Expected more than 6400 words with alphabets only, but got {count_alpha_only}"
'''
Write the parameterized test method test_words_starts_with to run against a set of multiple inputs
Mark the function as parameterize
Write the function to check the count of words starting with the input character is equal to the output
[('a',570),('e',327),('h',1296),('t',321)] is the set of inputs and outputs
'''
@pytest.mark.parametrize("char, expected_count", [
('a', 570),
('e', 327),
('h', 1296),
('t', 321)
])
def test_words_starts_with(unpick, char, expected_count):
words = unpick # This will call the fixture and get the list of words
count_starts_with_char = sum(1 for word in words if word.startswith(char))
assert count_starts_with_char == expected_count, f"Expected {expected_count} words starting with '{char}', but got {count_starts_with_char}"
0 comments:
Post a Comment