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 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: My question is, what is the second option for this? You should use the There is also a symmetrical 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. $ Json string and I'm using it inside the test tasks to avoid repeatable declarations because the same as
$ json string
$ 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); }}
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 () {// ...}}
teardown () which is called after every test, It is rare, however, that you will need to define this method.
Comments
Post a Comment