samedi 21 mars 2020

writing tests to test other tests in Pytest

I'm trying to write a command line tutorial to help users learn Pytest (as I'm learning it myself) and digital logic simultaneously. I need to figure out a good way of testing test files that a user will write to ensure their test functions work as they should.

The learner will have the task of writing test files, which test functions they're also tasked to write. By way of example, the goal would be for them to end up writing the following (or something equivalent based on test criteria):

test_not.py

solutions_path='./solutions/'
import sys
sys.path.append(solutions_path)
import re
import _not from _not

def test_allowed():
    f = open(solutions_path+"_not.py", "r")
    text=f.read()
    print(text)
    assert not (bool(re.search("\snot", text)))

def test_not():
    assert (_not(True)==(not True)) and (_not(False)==(not False))

solutions/_not.py

def _not(a):
    if a:
        return False
    return True

So what I need to figure out is an effective way of testing the file test_not.py such that it can be determined to pass a set of requirements like the following:

  1. create a file called test_not.py. This will test an alternate version of the not operation that you will write yourself.
  2. write a test that fails if the word not appears in the file to be tested.
  3. write a test that confirms that function called _not behaves the same as the standard not used by Python.

I'm new to Pytest myself and I'm not sure what options are available to me. Can anyone think of a way to do this?

Aucun commentaire:

Enregistrer un commentaire