This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021
Hello ~ I’m Milo! I am building an open source interface testing platform from 0 to 1, and I am also writing a complete tutorial corresponding to it. I hope you can support it. Welcome to pay attention to my public number test development pit goods, get the latest article tutorial!
review
In the previous section we supported SQL type constructors.
Next we’ll look at how to enrich our assertion types.
Basic assertion
Our previous assertion was incomplete, like the talisman sword score. Today we are going to try to refine the assertions.
Let’s look at the previous assertion types:
- Is equal to the
- Is not equal to
- Belong to
But these are often not enough, and we need to add more. Remember how we wrote earlier about comparing two JSON objects? We’re going to put it into practice today.
Write utils/json_compare. Py
Since we’ve talked about json comparisons before, here is the source code, and the JsonCompare class singleton wrapped.
import json
from app.utils.decorator import SingletonDecorator
@SingletonDecorator
class JsonCompare(object) :
def compare(self, exp, act) :
ans = []
self._compare(exp, act, ans, ' ')
return ans
def _compare(self, a, b, ans, path) :
a = self._to_json(a)
b = self._to_json(b)
if type(a) ! =type(b):
ans.append(f"{path}The types are inconsistent, respectively{type(a)} {type(b)}")
return
if isinstance(a, dict):
keys = []
for key in a.keys():
pt = path + "/" + key
if key in b.keys():
self._compare(a[key], b[key], ans, pt)
keys.append(key)
else:
ans.append(f"{pt}Does not exist in the actual results.")
for key in b.keys():
if key not in keys:
pt = path + "/" + key
ans.append(f"{pt}In the actual results.")
elif isinstance(a, list):
i = j = 0
while i < len(a):
pt = path + "/" + str(i)
if j >= len(b):
ans.append(f"{pt}Does not exist in the actual results.")
i += 1
j += 1
continue
self._compare(a[i], b[j], ans, pt)
i += 1
j += 1
while j < len(b):
pt = path + "/" + str(j)
ans.append(f"{pt}Is not present in the expected outcome.")
j += 1
else:
ifa ! = b: ans.append(f"{path}Inconsistent data:{a} "
f"! ={b}" ifpath ! ="" else
F "Inconsistent data:{a}! ={b}")
def _color(self, text, _type=0) :
if _type= =0:
# Description is green
return """<span style="color: #13CE66">{}</span>""".format(text)
return """<span style="color: #FF4949">{}</span>""".format(text)
def _weight(self, text) :
return """<span style="font-weight: 700">{}</span>""".format(text)
def _to_json(self, string) :
try:
float(string)
return string
except:
try:
if isinstance(string, str) :return json.loads(string)
return string
except:
return string
Copy the code
Override assertion mode
Take a look at the previous assertion:
Since we can’t add much, we’ll just add the usual ones.
Let’s add some checksums including/not included/included in/not included in/Length equal to/length greater than/length less than /JSON equal to. And using emoji to distinguish success from failure.
Take a look at the effect
This enriches our assertion tools a little bit, but we still have some JSON inclusion and other ways of comparing text to similarity assertions that we haven’t used yet, which we’ll show you at a later opportunity.
Today’s content to share here, interested friends can begin to write together! ~
(This installment is light, but the next installment brings a series of heavy Python timed tasks.)
Test. Pity. Fun
Front-end repository: github.com/wuranxu/pit…
Backend repository: github.com/wuranxu/pit…