php - Any alternatives with Global Variables? I don't want to use Global Variables in phpunit testing -


I am using phpunit to create some tests and I have a small problem. I have a $ Json string and I'm using it inside the test tasks to avoid repeatable declarations because the same as $ json string

I am now reading that we should not use global variables because this may face difficulties in code maintenance, so I am trying to explain another way to run the test.

About creating parameters for the function I thought that when I run phpunit, then this function is being run automatically.

This is my code:

  $ json = '{"name": "John", "age": "22",}'; $ Data = json_decode ($ json, true); $ Name = $ data ['name']; $ Age = $ data ['age']; Class User Test TestCase (Public Works) testCreateUserName () {Global $ json; Global $ name; $ This- & gt; Call ('POST', 'user', array (), array (), array (), $ json); $ This- & gt; AssertFalse ($ this- & gt; Customer & gt; GetResponse () - & gt; isOk ()); $ DecodedOutput = json_decode ($ this-> client- & gt; getResponse () - & gt; getContent ()); $ This- & gt; Emphasizing value ($ name, $ decodedOutput-> name, 'name input was incorrect'); $ This- & gt; AssertResponseStatus (201); }}   

My question is, what is the second option for this?

You should use the setUp () method that PHPUnit said earlier Performs each exam to start a class variable.

  Extended class UserTest TestCase {Protected $ json; Protected $ name; Protected Age; Public Function Setup () {$ this- & gt; Json = '{"name": "John", "age": 22}'; $ Data = json_decode ($ this-> json); $ This- & gt; Name = $ data ['name']; $ This- & gt; Age = $ data ['age']; } Public function testCreateUserName () {// ...}}   

There is also a symmetrical method teardown () which is called after every test, It is rare, however, that you will need to define this method.

See the PHPUnit Manual page for more information. I recommend to review the manual, how can you check that you rely on one another or provide a test in one test.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -